2014-09-19 8 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

+0

짧고 깨끗하거나 그냥 if 문 전에 age1int 및 age2int의 값을 출력하고 그 값을 볼 두 조건을 인쇄하고 –

+1

을 확인합니다. 그렇다면 당신은 –

답변

5

평등을 고려하지 않으므로 두 연령대가 같고 if 조건이 일치하지 않습니다. 나는 당신이 이것을 놓쳤다 고 생각한다. 다음 번에 디버거로 시도한 다음, 이런 종류의 이슈를 스스로 찾아 낼 수있다.

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

이렇게 변경해야합니다.

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

+1 좋은 캐치 :) –

+0

고맙습니다. 고맙습니다. – Insanovation

+0

@Insanovation 여러분을 환영합니다. –

관련 문제