2013-11-23 33 views
0

這是我的程序。我找不到任何錯誤,但是當我嘗試運行它時,有一個錯誤說明了將它除以零。我無法修復「線程異常」main「java.lang.ArithmeticException:/ by零」

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'爲零。 –

回答

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

相關問題