2014-04-01 43 views
0
public static void main(String[] args) { 

    //Creates the BigDecimal and scanner used 
    BigDecimal answer = new BigDecimal("0"); 
    Scanner scan = new Scanner(System.in); 

    //While statement to repeat if they dont answer 1 or 2 
    while (! answer.equals("1") && ! answer.equals("2")) { 
     //Asks user to input the number 1 or 2 
     System.out.print("Enter the number 1 or 2: "); 
     //Takes in users answer 
     answer = scan.nextBigDecimal(); 
    } 

    //Uses printf to print what they typed in 
    System.out.printf("You entered the number %s.", answer); 
    scan.close(); 

} 

此代碼不工作,我想知道爲什麼。問題似乎在while語句中,我似乎無法找到問題。請幫助我的while語句不能與我的BigDecimal一起工作

+0

請給我一個halp – Phteven

+1

請解釋一下「不工作」是什麼意思。它是否編譯?它是否編譯,但會引發錯誤?什麼是錯誤信息?它運行但沒有給出正確的答案?它給了什麼答案? –

回答

1

這裏

while (! answer.equals("1") && ! answer.equals("2")) { 

你的BigDecimal對象比較字符串 「1」 和 「2」。

這個怎麼樣?

while (! answer.equals(new BigDecimal("1")) && ! answer.equals(new BigDecimal("2"))) {