這是我的程序。我不知道哪裏除以零,所以我無法修復這個錯誤。無法修復<線程中的異常「main」java.lang.ArithmeticException:/ by zero>錯誤
Exception in thread "main" java.lang.ArithmeticException:/by zero
該程序應該反轉任何數字的數字。 ex。 57823 - > 32875 我無法工作。
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;
double 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/(Math.pow(10, t))) - ((n/(Math.pow(10, (t+1)))) * 10)) * 10));
}
println("The reverse number is " + total);
}
}
你有沒有看過異常堆棧跟蹤?它完全告訴你哪行代碼會導致異常!請參閱http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – isnot2bad
此外,此代碼與倒轉數字無關。要做到這一點,你只需要一個for和temp字符串。它會像3行代碼一樣。 –
重新安排你的while循環邏輯。 – Masudul