我有一个小功能,要求用户输入一个正数,而不是给他反向并计算其数字的总和。
代码的第一部分工作正常,但数字之和返回零如何解决这个问题?
我知道用户输入必须被要求输入一次而不是两次,所以如何修复此功能,因为它不接受用户输入。
package exam3Programing1_20_12_2010;
import java.util.Arrays;
import java.util.Scanner;
public class Question3 {
private int num;
private int sum;
private int reverse;
public int fill() {
System.out.println("enter the number");
Scanner in = new Scanner(System.in);
num = in.nextInt();
if (num < 0) {
System.out.println("enter a positive number");
}
while (num != 0) {
reverse = reverse * 10;
reverse = reverse + num % 10;
num = num / 10;
}
System.out.println("your reverse number is : " + reverse);
return num;
}
public void sumDigits() {
while (num != 0) {
sum = sum + num % 10;
num = num / 10;
}
System.out.println("sum of digits: " + sum);
}
public static void main(String[] args) {
Question3 q3 = new Question3();
q3.fill();
q3.sumDigits();
}
}
enter the number
1234
your reverse number is : 4321
sum of digits: 0
package exam3Programing1_20_12_2010;
import java.util.Arrays;
import java.util.Scanner;
public class Question3 {
private int num;
private int sum;
private int reverse;
public int fill() {
int myNumber = num;
System.out.println("enter the number");
Scanner in = new Scanner(System.in);
myNumber = in.nextInt();
if (myNumber < 0) {
System.out.println("enter a positive number");
}
while (myNumber != 0) {
reverse = reverse * 10;
reverse = reverse + myNumber % 10;
myNumber = myNumber / 10;
}
System.out.println("your reverse number is : " + reverse);
return num;
}
public void sumDigits() {
while (num != 0) {
sum = sum + num % 10;
num = num / 10;
}
System.out.println("sum of digits: " + sum);
}
public static void main(String[] args) {
Question3 q3 = new Question3();
q3.fill();
q3.sumDigits();
}
}
enter the number
1234
your reverse number is : 4321
sum of digits: 0
答案 0 :(得分:1)
那是因为你在反向和求和方法中共享相同的数字,当你第一次调用反向方法时,你继续将数字除以0,因此当你来求和方法时,你的数字是1,因此总和是0。
为了纠正这个问题,我建议你定义一个名为mynumber的局部变量,如:
public int fill() {
System.out.println("enter the number");
Scanner in = new Scanner(System.in);
num = in.nextInt();
if (num < 0) {
System.out.println("enter a positive number");
}
int myNumber = num;
while (myNumber != 0) {
reverse = reverse * 10;
reverse = reverse + myNumber % 10;
myNumber = myNumber / 10;
}
System.out.println("your reverse number is : " + reverse);
return num;
}
OUTPUT:
enter the number
1234
your reverse number is : 4321
sum of digits: 10
答案 1 :(得分:1)
您以一种相当无益的方式重用num
。尝试为方法定义局部变量,并仅在需要的地方使用实例字段。
另请注意,您的fill()
方法会返回您未使用的值。
这表明您应该完全删除num
字段。然后在num
方法中添加本地变量fill()
,然后更改sumDigits()
的签名以获取参数:
public void sumDigits(int num)
现在你的代码变成了
Question3 q3 = new Question3();
int n = q3.fill();
q3.sumDigits(n);
答案 2 :(得分:0)
您的num
成员通过读取数字并使用此循环进行初始化:
while (num != 0) {
reverse = reverse * 10;
reverse = reverse + num % 10;
num = num / 10;
}
您正在计算reverse
,但您在此过程中修改了num
的值。在循环之后,num
的值为0,之后,当您尝试使用它时,您已经丢失了它的值。试着这样做:
reverse = 0;
int tempNum = num;
while (tempNum != 0) {
reverse = reverse * 10;
reverse = reverse + tempNum % 10;
tempNum = tempNum / 10;
}
此代码将reverse
初始化为0,将num
的值存储为临时值,并在过程中修改tempNum
,因此保留num
的值没有变化,你可以在以后使用它。
答案 3 :(得分:0)
我稍微更新了您的代码并给出了正确的答案:
import java.util.Arrays; import java.util.Scanner;
公共课题问题3 {
private int num;
private int sum;
private int reverse;
public int fill() {
System.out.println("enter the number");
Scanner in = new Scanner(System.in);
num = in.nextInt();
if (num < 0) {
System.out.println("enter a positive number");
}
while (num != 0) {
reverse = reverse * 10;
reverse = reverse + num % 10;
num = num / 10;
}
System.out.println("your reverse number is : " + reverse);
return reverse;
}
public void sumDigits(int number) {
num = number;
while (num != 0) {
sum = sum + num % 10;
num = num / 10;
}
System.out.println("sum of digits: " + sum);
}
public static void main(String[] args) {
Question3 q3 = new Question3();
int number = q3.fill();
q3.sumDigits(number);
}
}