我怎么解决这个问题 [英] How can I solve this problem
问题描述
问题:
完成以数字格式取日期的程序,然后添加数字,直到
最后一位数字保持单身,将数字打印为幸运数字。例如,如果输入是777
那么7 + 7 + 7 = 21 = 2 + 1 = 3所以答案是3.
例1 :
输入日期(ddmmyy):
131083
您的幸运数字是:
7 >
示例2:
输入日期(ddmmyy):
111111
您的幸运数字是:
6
我尝试了什么:
i试过
Question:
Complete the program which takes date in number format and then makes its addition of the digits until the
last digit remains single and the print the number as lucky number . eg if input is 777
then 7+7+7 =21 = 2+1 =3 so the answer is 3.
Example1:
Enter the date(ddmmyy):
131083
Your lucky number is:
7
Example2:
Enter the date(ddmmyy):
111111
Your lucky number is:
6
What I have tried:
i tried
<pre>import java.io.*;
class digitsum{
public static void main(String arg[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the date(ddmmyy):");
int number =Integer.parseInt(br.readLine());
System.out.println("Your lucky number is:");
//doSum(number);
System.out.println(doSum(number));
}//main
static int doSum(int num)
{
int ans=0;
if(num%10==0)
{
return num;
}
else
{
ans = num%10 + doSum(num/10);
}
if(ans<10)
{
return ans;
}
else
{
doSum(ans);
}
return ans;
}
}
此代码工作正常,但
$ 7 $ b为777输入它显示21
但我想要
7 + 7 + 7 = 21 = 2 + 1 =3作为最终的ans
this code works fine but
for an 777 input it showing 21
but i want
7+7+7 = 21 = 2+1 = "3" as final ans
推荐答案
你应该返回一个doSum
来电:
You should return adoSum
call:
static int doSum(int n)
{
if (n <10)
return n;
return doSum( n%10 + n/10);
}
static int doSum(int num)
{
int ans=0;
if(num%10==0) // wrong test try with 7770
{
return num;
}
else
{
ans = num%10 + doSum(num/10);
}
if(ans<10)
{
return ans;
}
else
{
doSum(ans); // Here you loose the result of doSum
}
return ans;
}
有一个工具可以让你看到你的代码在做什么,它的名字是调试的。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。
当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器。
使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。
调试器 - 维基百科,免费的百科全书 [ ^ ]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]
调试器在这里向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。
调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
这篇关于我怎么解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!