2016-07-06 7 views
1

무한 루프를 만들었고 루프를 시작할 때마다 취소 버튼이 작동하지 않고 대화 상자가 계속 팝업됩니다. 나는이 권리를 포맷하지 않은 경우취소 버튼 기능을 만드는 방법

여기

String buffer = " "; //Input a string into console 
    boolean badInput = true; 
    boolean badInput2 = true; 
    String idNum = ""; //ask for id number 
    String skill = ""; // ask for skill number 
    int skillInt = 0; // skill is an int 

    //prompt user for file location 
    File loc = new File(JOptionPane.showInputDialog(null, "Please provide the file location: ")); 
    RandomAccessFile store = new RandomAccessFile(loc, "rw"); 

    //prompt user for a command 
    String cmd = "start"; 
    int recLocation = 0; 
    while(cmd.compareToIgnoreCase("end")!=0){ //stay in program (loop) until end command is given 
     cmd = JOptionPane.showInputDialog(null, "Please input a command (new, old or end): "); 

     //creating new entry 
     if(cmd.compareToIgnoreCase("new")==0){ 
      while(badInput){ //keep them in loop until they give the input in the right format 
       idNum = JOptionPane.showInputDialog(null, "Please input an ID number (1 - 20): "); 
       // else JOptionPane.CANCEL_OPTIONsetDefaultCloseOperation(JOptionPane.EXIT_ON_CLOSE); 
       try{ 
        //corresponding int for ID number, which becomes the record location 
        //if number is not 1-20, code below 
        recLocation = Integer.parseInt(idNum); 
        if(recLocation<1 || recLocation>20){ 
         System.out.println("Please check that your number is between 1 and 20."); 
        }else{ 
         badInput = false; 
         break; 
        } 

       } 
       catch(NumberFormatException NF){ // if input isnt a number 
        System.out.println("Please check that your number is in the correct format."); 
       } 
      } 

      //ask for names 
      String pName = JOptionPane.showInputDialog(null, "Please input a player name: "); 
      String tName = JOptionPane.showInputDialog(null, "Please input a team name: "); 

      //ask for skill level 
      while(badInput2){ //keep them in the loop until they give the input in the right format 
       skill = JOptionPane.showInputDialog(null, "Please input a skill level (0 - 99): "); 
       try{ 
        //corresponding int for skill number, to check if in the right format 
        skillInt = Integer.parseInt(skill); 
        if(skillInt<0 || skillInt>99){ 
         System.out.println("Please check that your number is between 0 and 99."); 
        }else{ 
         badInput2 = false; 
         break; 
        } 

       } 
       catch(NumberFormatException NF){ //exception or error thrown if input is not in correct format 
        System.out.println("Please check that your number is in the correct format."); 
       } 
      } 

      String date = JOptionPane.showInputDialog(null, "Please input today's date (ex: 25Jun2014): "); 

      //formatting id number 
      if (idNum.length() < 2){ 
       idNum = idNum+buffer; 
      } 
      //formatting player name 
      if (pName.length() > 26){ 
       pName = pName.substring(0, 26); 
      } else { 
       while(pName.length() < 26){ 
        pName = pName+buffer; 
       } 
      } 

      //formatting team name 
      if (tName.length() > 26){ 
       tName = tName.substring(0, 26); 
      } else { 
       while(tName.length() < 26){ 
        tName = tName+buffer; 
       } 
      } 
      //formatting date 
      if (date.length() > 9){ 
       date = date.substring(0, 9); 
      } else { 
       while(date.length() < 9){ 
        date = date+buffer; 
       } 
      } 
      //formatting skill 
      if (skill.length() < 2){ 
       skill = skill+buffer; 
      } 
      //create full, identifying string 
      String fullRecord = idNum + " " + pName + tName + skill + " " + date; 
      store.seek((RECORD_LENGTH+2) * (recLocation-1)); 
      store.writeUTF(fullRecord); 

      //reset booleans 
      badInput = true; 
      badInput2 = true; 

     } 

     //accessing old entry 
     if(cmd.compareToIgnoreCase("old")==0){ 
      idNum = JOptionPane.showInputDialog(null, "Please input an ID number (1 through 20): ");  
      recLocation = Integer.parseInt(idNum); 
      store.seek((RECORD_LENGTH+2)*(recLocation-1)); 
      String fullRecord = store.readUTF(); 

      //interpret information from full string 
      try 
      {idNum = fullRecord.substring(0, 5); 
      String pName = fullRecord.substring(5, 31); 
      String tName = fullRecord.substring(31, 57); 
      skill = fullRecord.substring(57, 62); 
      String date = fullRecord.substring(62, 71); 
      System.out.println("ID: "+idNum+" NAME: "+pName+" TEAM: "+tName+" SKILL: "+skill+" DATE: "+date); 
      } 
      catch(StringIndexOutOfBoundsException S){ 
       System.out.println("No record found at that location."); 
      } 
     } 

     // JOptionPane.showMessageDialog(null, "good bye"); 
    } 

죄송 코드를합니다. 내 첫 시간이야. 루프를 종료하기 위해 취소 버튼을 어떻게 얻을 수 있는지 궁금합니다. 나는 if (cmd == null) System.exit(0);을 사용하여 시도했지만 작동하지 않는 것 같습니다. 나는 자바에서 정말 초보자 다. 그래서 나에게는 작은 경험이있다. 그런 다음 JOptionPane.showInputDialog 중 하나()의 반환 그들이 당신을 취소했던 의미하는 null의 경우 https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String)

while(!cmd.equalsToIgnoreCase("end")) { 

:

+0

이 페이지를 참조하십시오 : http://stackoverflow.com/help/mcve. 여기서 핵심은 MINIMAL입니다. 즉, 문제를 재현 할 수있는 최소한의 코드를 생성하십시오. 루프의 중간에있는 모든 것들은 관련이 없으며 질문을 읽기가 어렵게 만듭니다. – nhouser9

답변

1

우선 대신 compareIgnoreCase 당신의 문자열 비교 equalsIgnoreCase을 사용하는 것이 좋습니다 것 cmd 문자열을 "end"로 설정하는 if 문을 가질 수 있습니다. 제 생각에는 아마도 루프를 단순화하려고 노력해야 할 것입니다. 왜냐하면 루프가 하나의 블록에 모두 포함되어 있기 때문입니다. 따라서 디버깅하기가 어렵습니다.