2014-06-22 2 views
1

내 프로그램에서 JOptionPane.showConfirmDialog를 사용하여 사용자가 시뮬레이션을 종료 할 것인지 묻는 메시지를 표시합니다. "아니오"를 클릭하면 prgoram은 "계속되는 siumlation ..."을 출력하고 계속 프로그램을 계속 진행하지만 아무 것도 클릭하지 않으면 "계속 시뮬레이션"이 출력되고 "유지 보수가 수행되지 않습니다" 시뮬레이션 종료 ... " 어쨌든 이것이 왜 일어나는 지 알 수 있습니까? 첫 번째 대화 상자에서 "아니오"를 클릭하면 두 번째 대화 상자를 사용하지 않았기 때문버튼을 클릭하면 왜 내 프로그램이 잘못된 출력을 내 보냅니 까?

  if(main.equalsIgnoreCase("N") | main.equalsIgnoreCase("NO")){ 
       result = JOptionPane.showConfirmDialog(null, "WARNING! Are you sure you don't want to carry out maintenance? The simulation will stop if you click YES. ", "TERMINATE THE SIMULATION? ", JOptionPane.YES_NO_OPTION); 
       if(JOptionPane.NO_OPTION == result){ 
        System.out.println("Continuing Simulation..."); 
       } 
       if (JOptionPane.YES_OPTION == result) { 
        Result2 = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit the simulation?", "Are You Sure?", JOptionPane.YES_NO_OPTION); 
       } 
       if(JOptionPane.YES_OPTION == Result2){ 
        System.out.println("Maintenance will not be carried out, exiting Simulation..."); 
        try{ 
         System.exit(0); 
        } 
        finally{ 
         System.err.println("Exiting..."); 
         System.exit(1); 
        } 
       } 
       if (JOptionPane.NO_OPTION == Result2) { 
        System.out.println("Continuing simulation..."); 
      } 
      } 

답변

3

, 당신은 Result2에 대한 값을 할당하지 않습니다. 그러나이 if 문 if(JOptionPane.YES_OPTION == Result2){에 여전히 Result2 값을 사용하고 있습니다.

Result2이 인스턴스 변수이고 아직 값을 지정하지 않은 경우 문제는 int의 기본값 인 0입니다.

상수 JOptionPane.YES_OPTION의 값도 0입니다. 따라서 if-statement는 true으로 평가되며 유지 보수 메시지를 인쇄하고 종료합니다.

당신은 둥지 경우 생성 - 문 당신이 그것에 의미있는 값을 할당 할 때 Result2의 값에 대한 검사 만 다음과 같이 실행되도록해야합니다

if (JOptionPane.YES_OPTION == result) { 
    Result2 = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit the simulation?", "Are You Sure?", JOptionPane.YES_NO_OPTION); 
    if(JOptionPane.YES_OPTION == Result2){ 
     System.out.println("Maintenance will not be carried out, exiting Simulation..."); 
     try{ 
      System.exit(0); 
     } 
     finally{ 
      System.err.println("Exiting..."); 
      System.exit(1); 
     } 
    } 
    if (JOptionPane.NO_OPTION == Result2) { 
     System.out.println("Continuing simulation..."); 
    } 
} 
0

하면이 시도 :

if(main.equalsIgnoreCase("N") | main.equalsIgnoreCase("NO")){ 
      result = JOptionPane.showConfirmDialog(null, "WARNING! Are you sure you don't want to carry out maintenance? The simulation will stop if you click YES. ", "TERMINATE THE SIMULATION? ", JOptionPane.YES_NO_OPTION); 
      if(JOptionPane.NO_OPTION == result){ 
       System.out.println("Continuing Simulation..."); 
      }else{ 
      if (JOptionPane.YES_OPTION == result) { 
       Result2 = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit the simulation?", "Are You Sure?", JOptionPane.YES_NO_OPTION); 
      } 
      if(JOptionPane.YES_OPTION == Result2){ 
       System.out.println("Maintenance will not be carried out, exiting Simulation..."); 
       try{ 
        System.exit(0); 
       } 
       finally{ 
        System.err.println("Exiting..."); 
        System.exit(1); 
       } 
      } 
      if (JOptionPane.NO_OPTION == Result2) { 
       System.out.println("Continuing simulation..."); 
      } 
     }} 
비트 단위의 포함을 사용 |

당신은

0

if(JOptionPane.NO_OPTION == result)이 (main.equalsIgnoreCase ("NO") main.equalsIgnoreCase ("N"))하면 이후에 다른 둘 필요가 사용 OR 연산자 "|". 논리 OR 연산자 "||" 대신 :

if(main.equalsIgnoreCase("N") || main.equalsIgnoreCase("NO")){ 
관련 문제