이것은 내 프로그램입니다. 나는이 오류를 고칠 수 없으므로 0으로 나누는 곳을 모른다.스레드 "main"예외를 해결할 수 없습니다. java.lang.ArithmeticException :/by zero> 오류
Exception in thread "main" java.lang.ArithmeticException:/by zero
이 프로그램은 숫자의 자릿수를 뒤집어서 표시합니다. 예. 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