2013-11-23 3 views
0

이것은 내 프로그램입니다. 내가 잘못한 것을 찾을 수는 없지만 그것을 실행하려고 할 때 메신저가 0으로 나눈다는 오류가 있습니다."스레드 예외"메인 "java.lang.ArithmeticException :/0으로 해결할 수 없습니다"

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 
+3

당신은'^'이 무엇을한다고 생각하니? –

+0

'10^t'는't = 10'에 대해 0입니다. –

답변

3

^ - XOR의 opertion이 아닌 정도입니다! 당신이 필요합니다 Math.pow(number, degree);

+0

이제 다른 문제가 있습니다. total = (total + ((n/(Math.pow (10, t)) - ((n/(Math.pow (10, (t + 1)))) * 10)) * 10)); "가능한 정밀도 손실"오류 – user3025598

+0

예, java의 나누기 결과가 "double"유형입니다. "total"의 유형을 "double"로 변경하거나 계산 결과를 "int"로 변환하십시오. – Mark

+0

정말 그럴 수 없어 ... – user3025598