2014-09-19 100 views
0

字符串被解析爲整數,所以if語句的條件已正確設置。爲什麼if語句沒有運行?爲什麼沒有出現帶有響應的MessageDialog?如果沒有運行語句

class process{ 
    public static void whoIs(){ 

     JFrame frame=new JFrame("The Oldest"); 
      String a=JOptionPane.showInputDialog(frame, "Enter, please, the first name and age:", "QUIZ: Who is the Oldest", JOptionPane.QUESTION_MESSAGE); 
      String b=JOptionPane.showInputDialog(frame, "Enter, please, the second name and age:", "QUIZ: Who is the Oldest", JOptionPane.QUESTION_MESSAGE); 


      String age1=a.replaceAll("[^\\d]",""); 
      String age2=a.replaceAll("[^\\d]",""); 



      String name1=a.replaceAll("\\d",""); 
      String name2=b.replaceAll("\\d",""); 


      int age1int=Integer.parseInt(age1); 
      int age2int=Integer.parseInt(age2); 



      if (age1int>age2int){ 
       JOptionPane.showMessageDialog(frame, name1+ " is the oldest!", "QUIZ: Who is the Oldest?", JOptionPane.INFORMATION_MESSAGE); 
      } 

      if (age2int>age1int) { 
       JOptionPane.showMessageDialog(frame, name2+ " is the oldest!", "QUIZ: Who is the Oldest?", JOptionPane.INFORMATION_MESSAGE); 
      } 

    } 
} 
+1

這是微不足道的 - 只是對其進行調試,代碼短,乾淨 – nogard 2014-09-19 09:59:00

+0

或只是打印兩種條件和檢查 – 2014-09-19 10:00:45

+1

只是if語句之前打印age1int和age2int的值,看看它們的值。那麼你會明白 – 2014-09-19 10:00:59

回答

5

你的兩個年齡都一樣,你的條件不匹配,因爲你不考慮平等。我想你錯過了這一點,下次嘗試使用調試器,然後你可以自己識別這些類型的問題。

String age1=a.replaceAll("[^\\d]",""); 
String age2=a.replaceAll("[^\\d]",""); 

這應該將其更改爲以下內容。

String age1=a.replaceAll("[^\\d]",""); 
    String age2=b.replaceAll("[^\\d]",""); 
+2

+1好聽:) – 2014-09-19 10:03:06

+0

謝謝。非常感謝你。 – Insanovation 2014-09-19 10:04:24

+0

@Insanovation歡迎您 – 2014-09-19 10:04:59