2013-12-15 66 views
1
package iCanDoIt; 

    import java.util.Scanner; 

    import javax.swing.JOptionPane; 

    public class Practice { 

     public static void main(String[] args) { 

     String msg=JOptionPane.showInputDialog("Enter a number"); 
     int num1=Integer.parseInt(msg); 
     String msg2=JOptionPane.showInputDialog("enter another number"); 
     int num2=Integer.parseInt(msg2); 
     int addition=num1+num2; 

     String msg3=JOptionPane.showInputDialog("What is" + num1 + "+ " + num2 + " ? "); 

     int answer=Integer.parseInt(msg3); 

     while(num1+num2!=answer){ 

      JOptionPane.showMessageDialog(null,"try again"); 
      String answer2=JOptionPane.showInputDialog("wrong. what is " + num1 + "+" + num2 + "?"); 
      int answer3=Integer.parseInt(answer2); 

     if (num1+num2==answer3) 

      JOptionPane.showMessageDialog(null,"GREAT job"); 


     } 
     System.exit(0); 

     } 


    } 

我是JAVA的初學者。JOptionPane和While循環(用戶輸入添加)

掃描儀輸入看起來很容易理解,但由於某種原因,我使用JOptionPane很困難。

不管怎樣,我的問題是...即使我得到的答案是正確的,我不斷收到「再試一次」

你能告訴我什麼,我做錯了什麼?

回答

0

你永遠不會更新answer

更改此

int answer3=Integer.parseInt(answer2); 
if (num1+num2==answer3) 

這個

answer=Integer.parseInt(answer2); 
if (num1+num2==answer) 

你也得到移動你的最終消息對話框外循環擺脫if (num1+num2==answer)檢查:

int answer=Integer.parseInt(msg3); 

    while(num1+num2!=answer){ 

     JOptionPane.showMessageDialog(null,"try again"); 
     String answer2=JOptionPane.showInputDialog("wrong. what is " + num1 + "+" + num2 + "?"); 
     answer=Integer.parseInt(answer2); 

    } 
    JOptionPane.showMessageDialog(null,"GREAT job"); 
    System.exit(0); 
+0

@ DaoWen-啊!非常有意義。非常感謝!!!! :) :) :) – user3104903