如果语句没有运行

时间:2014-09-19 09:57:44

标签: java if-statement joptionpane

字符串被解析为整数,因此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 个答案:

答案 0 :(得分:5)

你的两个年龄相同,你的if条件不匹配,因为你没有考虑平等。我想你错过了这个,下次尝试使用调试器,然后你可以自己识别这类问题。

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

这应该将此更改为以下内容。

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