2013-04-07 2 views
0

여기서 일어나는 일에 대해 조금 혼란 스럽습니다. 이 오류 트랩의 요점은 예를 들어, 사용자가 4 자리 숫자 대신 3 개의 숫자/문자를 입력한다는 것입니다. 이 오류 함정은 사용자가 올바르게 이해할 때까지 질문을 반복하도록 설계되었습니다. 그러나 대신 오류 메시지가 반복됩니다. 아무도 무슨 일이 일어나고 있는지에 관해서 어떤 조언을 해줄 수 있습니까?오류 트래핑 도움말; 메시지는 계속 반복됩니다.

JFrame Error = new JFrame(); 

String input = JOptionPane.showInputDialog(null,"Enter the 4 digit resistor values:"); 

while (true){ 
    try{ 

    int numInput = Integer.parseInt (input); 

    if (numInput >= 1000) { 
    break; 
    } 
    else { 
     JOptionPane.showMessageDialog(Error,"Invalid Input."); 
    } 
    } 
    catch (Exception e){ 
    JOptionPane.showMessageDialog(Error,"Invalid Input."); 

    } 
} 
+1

입력을 요청하는 줄이 루프 안에 없습니다. –

답변

0

입력을 루프에 요청하려면 코드를 이동해야합니다.

JFrame Error = new JFrame(); 

String input = null; 

while (true){ 
    try{ 

    input = JOptionPane.showInputDialog(null,"Enter the 4 digit resistor values:"); 
    int numInput = Integer.parseInt (input); 

    if (numInput >= 1000) { 
    break; 
    } 
    else { 
     JOptionPane.showMessageDialog(Error,"Invalid Input."); 
    } 
    } 
    catch (Exception e){ 
    JOptionPane.showMessageDialog(Error,"Invalid Input."); 

    } 
} 
-1

당신은 루프 내부에 새로운 값을 을 요청해야합니다. 다음과 같이 변경하십시오 :

String input; 

try { 
    while (true){ 
     input = JOptionPane.showInputDialog(null,"Enter the 4 digit resistor values:"); 

     int numInput = Integer.parseInt (input); 

     if (numInput >= 1000) { 
      break; 
     } 
     else { 
      JOptionPane.showMessageDialog(Error,"Invalid Input."); 
     } 
    } 
} catch (Exception e) { 
    JOptionPane.showMessageDialog(Error,"Invalid Input."); 
} 
+1

try-catch는 어떻게 필요하지 않습니까? 사용자가 abcd를 입력하면 어떻게됩니까? (이 유형의 입력 *은 질문에서 다루었습니다)? – chris

+0

@chris 오, 고칠 것입니다. – Mordechai

0

JacobM이 언급했듯이 while 루프 내부의 입력을 요청해야합니다. catch 절이 종료되면 코드가 수행하는 다음 작업이 while 루프의 첫 번째 작업이됩니다.

JFrame Error = new JFrame(); 

while (true){ 
    String input = JOptionPane.showInputDialog(null,"Enter the 4 digit resistor values:"); 
    try{ 

    int numInput = Integer.parseInt (input); 

    if (numInput >= 1000) { 
    break; 
    } 
    else { 
     JOptionPane.showMessageDialog(Error,"Invalid Input."); 
    } 
    } 
    catch (Exception e){ 
    JOptionPane.showMessageDialog(Error,"Invalid Input."); 

    } 
} 
관련 문제