Java - 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字之和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28158846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits
提问by tmwill0422
I have seen this question asked a few times, but all of the responses have included functionality that I haven't learned yet in this class and am I sure there must be a way to do it with only what I have learned. No arrays, etc... just loops and prior. I am not really looking for the answer, but just some direction. I have included the code I have already done. The program needs to be able to hand negative numbers, the sum and then print in the proper order. Right now my code does everything except print in the proper order. I understand why it is printing in reverse order (because the loop gets rid of and then prints the last number in the int), but I can't seem to figure out a way to change it. I have tried converting it to a string, char and just can't get it. Please take a look, and provide some guidance if you don't mind. thank you in advance.
我已经看到这个问题问了几次,但所有的回答都包含了我在这门课上还没有学到的功能,我确定一定有一种方法可以只用我学到的东西来做到这一点。没有数组等......只是循环和先验。我并不是真的在寻找答案,而只是寻找一些方向。我已经包含了我已经完成的代码。该程序需要能够处理负数、总和,然后以正确的顺序打印。现在我的代码除了按正确的顺序打印外,什么都做。我明白为什么它以相反的顺序打印(因为循环去掉然后打印 int 中的最后一个数字),但我似乎无法找到改变它的方法。我曾尝试将其转换为字符串、字符,但无法获取。请看一看,如果你不这样做,请提供一些指导 不介意。先感谢您。
public static void main(String[] args) {
int num;
int sum;
int temp;
System.out.print("Enter an integer, positive or negative: ");
num = keyboard.nextInt();
System.out.println();
if (num < 0)
num = -num;
sum = 0;
while (num > 0) {
temp = num;
sum = sum + num % 10; //Extracts the last digit and adds it to the sum
num = num / 10; //removes the last digit
System.out.print(temp % 10 + " ");
}
System.out.println(" and the sum is " + sum);
}
}
采纳答案by JimW
Not knowing what they've taught you in class so far, an easy albeit inefficient thing to do is to recreate the number as string.
到目前为止,不知道他们在课堂上教了你什么,一个简单但效率低下的方法是将数字重新创建为字符串。
String numbers = "";
while (num > 0) {
temp = num;
sum = sum + num % 10; //Extracts the last digit and adds it to the sum
num = num / 10; //removes the last digit
// System.out.print(temp % 10 + " ");
numbers = (temp % 10) + " " + numbers;
}
System.out.println(numbers + "and the sum is " + sum);
You were already grabbing the ones digit with (temp % 10) and then right shifting the original number with num = num / 10. There are other data structures you could use like a Stack or a LinkedList that are more natural to use in a situation like yours, or you could use a StringBuilder to append the digits to the end and then use the reverse() method to get them back in the correct order, but those data structures probably come after Arrays which you mentioned you didn't know.
您已经使用 (temp % 10) 获取个位数,然后使用 num = num / 10 将原始数字右移。您还可以使用其他数据结构,例如 Stack 或 LinkedList,它们在某种情况下使用起来更自然像您的一样,或者您可以使用 StringBuilder 将数字附加到末尾,然后使用 reverse() 方法将它们恢复为正确的顺序,但这些数据结构可能出现在您提到的您不知道的数组之后。
Given those constraints, I used String concatenation. In general here is what happens:
鉴于这些限制,我使用了字符串连接。一般情况下会发生以下情况:
String numbers = "";
num = 123;
digit = num % 10; // digit=3
num /= 10; // num=12
numbers = digit + " " + numbers; // numbers="3 " uses old value on right side of the equals
// next iteration
digit = num % 10; // digit=2
num /= 10; // num=1
numbers = digit + " " + numbers; // numbers="2 3 " see how the digit is put to the left of the old value
// last iteration
digit = num % 10; // digit=1
num /= 10; // num=0
numbers = digit + " " +numbers; // numbers="1 2 3 " notice there is an extra space at the end which is ok for your example
回答by LeonF
Set a counter, loop num/10, if result>0 counter++. In the end, counter+1 will be the number of digits
设置一个计数器,循环 num/10,如果 result>0 counter++。最后, counter+1 将是位数