字符串被解析爲整數,所以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);
}
}
}
這是微不足道的 - 只是對其進行調試,代碼短,乾淨 – nogard 2014-09-19 09:59:00
或只是打印兩種條件和檢查 – 2014-09-19 10:00:45
只是if語句之前打印age1int和age2int的值,看看它們的值。那麼你會明白 – 2014-09-19 10:00:59