►斜体或粗体
► 代码缩进 4 个空格 ► 反引号转义like _so_
► 将 > 放在行首引用 ► 建立链接
问问题
739 次
2 回答
2
您的代码有问题:
您永远不会将用户输入放在 num 数组中,而 for 循环只是一个主动等待,
现在就试试:
public static void main(String[] args)
{
//call for input
System.out.println("Please Enter a 3-digit number..");
Scanner in = new Scanner(System.in);
int val = in.nextInt();
int[] num = new int[3];
if(val<=99) // is not a 3 digit number
return;
int i =2;
while (val > 0) {
num[i]=(val%10);
val = val / 10;
i--;
}
System.out.println("The Sum of the numbers is " + (num[0] + num[1]+ num[2]));
System.out.println("The Reverse of the numbers is " + num[2] +""+ num[1]+""+ num[0]);
}
输入 1 - 2 -3 为您提供:
Please Enter a 3-digit number..
1
2
3
The Sum of the numbers is 6
The Reverse of the numbers is 321
于 2013-08-27T02:01:16.207 回答
1
尝试这个
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int sum = 0;
int rev = 0;
while (n > 0) {
int rem = n % 10;
sum += rem;
rev = rev * 10 + rem;
n = n / 10;
}
System.out.println("The Sum of the numbers is " + sum);
System.out.println("The Reverse of the numbers is " + rev);
于 2013-08-27T04:20:04.927 回答