这是我的计划。我发现它没有任何问题,但是当我试图运行它时会出现一个错误,即我将其除以零。
import acm.program.*;
public class ReverseDigits extends Program {
public void run(){
println("This program reverses the digits in an integer.");
int n = readInt("Enter a positive integer: ");
int x = 10;
int t = 1;
int total = 0;
//Finds the number of digits
while (n > 0){
while (n % x != 0) {
t = t + 1;
x = x * 10;
}
}
//In case the number has one digit the new number is the same
if(t == 1) {
total = n;
}
//Creating the new number
while (t > 1) {
t=t-1;
total = (total + ((( n / (10^t)) - ((n / (10 ^ (t+1))) * 10 )) * 10));
}
println("The reverse number is " + total);
}
}
尝试运行时遇到此错误,但我找不到问题。
Exception in thread "main" java.lang.ArithmeticException: / by zero
答案 0 :(得分:3)
^
- 是一个XOR操作,而不是程度!您需要Math.pow(number, degree);