2016-07-13 131 views
2

這個程序應該接受用戶的輸入,把它放到一個文本文件中,然後在需要的時候能夠拉動信息(尚未實現)。程序打開並獲取值並保存它們,但是當我關閉程序時,會出現一個新窗口,並重復此操作。有關如何阻止此問題的任何幫助?這可能只是一個失落的地方,但我無法爲我的生活解決它。當我嘗試關閉它時,循環程序。我如何解決這個無限循環?

public class FirstTr { 

final private static int MAX_RECORD_NUMBER = 71; 
final private static int MAX_PLAYER_NUMBER = 99; 
final private static char PLAYER_NAME = 26; 
final private static char TEAM_NAME = 26; 
final private static int SKILL_LEVEL = 5; 
final private static int DRAFT_DATE = 9; 
final private static int RECORD_LENGTH = 16; 
final private static int PLAYER_ID = 20; 
final private static int ID_Length = 5; 


public static void main(String[] args) throws FileNotFoundException, IOException { 


    File loc = new File("C:\\Users\\Desktop\\Exc2.1.txt"); 
    RandomAccessFile store = new RandomAccessFile(loc, "rw"); 
    String id1 = ""; 
    String id2 = ""; 
    String id3 = ""; 
    String id4 = ""; 
    String id = ""; 
    String Description = ""; 
    int recLocation = 0; 
    String cmd = "Start"; 
    String where = "0"; 




    cmd= JOptionPane.showInputDialog(null, " Please type in a command : new, old or exit"); 

    if (cmd.compareToIgnoreCase("end")== 0){ 
     store.close(); 
     System.exit(0); 

    } 

    while (cmd.compareToIgnoreCase("end")!= 0){ 

     if (cmd.compareToIgnoreCase("new") == 0){ 
      //Ask user for ID 1-20, read ID 
      try{ 
       id1 = JOptionPane.showInputDialog(null,"Enter ID(1-20):"); 
       recLocation= Integer.parseInt(id1); 
       assert Integer.MAX_VALUE == PLAYER_ID; 

       JOptionPane.showInputDialog(null, "The ID IS "+ id1); 
      } 
      catch (Exception e){ 
       JOptionPane.showInputDialog(null, "SORRY THIS IS NOT AN INTEGER PLEASE PRESS ENTER TO CONTINUE"); 

      } 
      try{ 
       //Ask user for player name, read name 
       id2 = JOptionPane.showInputDialog(null, "Enter a players name"); 
       assert id.length()== PLAYER_NAME; 
       JOptionPane.showInputDialog("The players name is " + id2 + " press enter to continue"); 
       store.writeUTF(id2); 
      } 
      catch (Exception e){ 
       JOptionPane.showInputDialog(null, "SORRY SOMETHING WENT WRONG PLEASE PRESS ENTER TO CONTINUE"); 

      } 
      try{ 
       //ask for player team name, read team name 
       id3 = JOptionPane.showInputDialog(null, "Enter a players team name"); 
       JOptionPane.showInputDialog("The players team name is " + id3 + ", press enter to continue"); 
       assert id.length()== TEAM_NAME; 
       store.writeUTF(id3); 
      } 
      catch (Exception e){ 
       JOptionPane.showInputDialog(null, "SOMETHING WENT WRONG PLEASE PRESS ENTER TO CONTINUE"); 

      } 

      //enter player skill level, read skill level(0-99) 
      try{  
       id4 = JOptionPane.showInputDialog(null,"Enter a players skill level (0-99)"); 
       recLocation = Integer.parseInt(id4); 
       JOptionPane.showInputDialog("The players skill level " + id4 + " press enter to continue"); 
      } 
      catch (Exception e){ 
       JOptionPane.showInputDialog(null, "SORRY THIS IS NOT AN INTEGER PLEASE PRESS ENTER TO CONTINUE"); 

      } 
      //enter player skill level, read skill level 
      try{  
       id = JOptionPane.showInputDialog(null, "Enter todays Date"); 
       JOptionPane.showInputDialog("Today is " + id + " press enter to continue"); 
       assert id.length()== DRAFT_DATE; 
       store.writeUTF(id); 
      } 
      catch (Exception e){ 
       JOptionPane.showInputDialog(null, "SORRY THIS IS NOT AN INTEGER PLEASE PRESS ENTER TO CONTINUE"); 
       continue; 
      } 
      //convert ID and skill level to string(char-5) 


     } 



     //if command is old, ask for ID and read Id, then use ID to retrieve record, display the record formatted for readability 


     if (cmd.compareToIgnoreCase("old") == 0) { 
      try{ 
       where = JOptionPane.showInputDialog(null, "Enter player:"); 
       recLocation = Integer.parseInt(where); 
       store.seek((PLAYER_ID) * (recLocation-1)); 
       Description = store.readUTF(); 

       JOptionPane.showMessageDialog(null, Description); 
      } 
      catch(Exception e){ 
       JOptionPane.showInputDialog("Sorry there is no player try again"); 
       continue; 
      } 
     } 
    } 
} 

回答

0

您只有一次讀取cmd的輸入,然後它永遠不會被重新分配。所以,如果第一個while的條件(cmd.compareToIgnoreCase("end")!= 0)爲真,那麼它將永遠循環。

0

你的while loop是罪魁禍首。循環從字面上繼續前進,因爲沒有什麼會改變條件。

如果您想要將此條件(cmd.compareToIgnoreCase("end")!= 0)更改爲true,您的腳本將以您希望的方式終止!

注意:您不會將"exit"這個詞分配給一個命令!也許爲此做出一個命令將會使劇本做你想做的事!

編輯:嘗試改變"end""exit"在這些片段在這裏!

if (cmd.compareToIgnoreCase("end")== 0){ 
     store.close(); 
     System.exit(0); 

while (cmd.compareToIgnoreCase("end")!= 0) 

EDIT2:通過添加if語句while循環的底部,它應該退出上述!

while (cmd.compareToIgnoreCase("exit")!= 0){ 

    if (cmd.compareToIgnoreCase("new") == 0){ 
     //Ask user for ID 1-20, read ID 
     try{ 
      id1 = JOptionPane.showInputDialog(null,"Enter ID(1-20):"); 
      recLocation= Integer.parseInt(id1); 
      assert Integer.MAX_VALUE == PLAYER_ID; 

      JOptionPane.showInputDialog(null, "The ID IS "+ id1); 
     } 
     catch (Exception e){ 
      JOptionPane.showInputDialog(null, "SORRY THIS IS NOT AN INTEGER PLEASE PRESS ENTER TO CONTINUE"); 

     } 
     try{ 
      //Ask user for player name, read name 
      id2 = JOptionPane.showInputDialog(null, "Enter a players name"); 
      assert id.length()== PLAYER_NAME; 
      JOptionPane.showInputDialog("The players name is " + id2 + " press enter to continue"); 
      store.writeUTF(id2); 
     } 
     catch (Exception e){ 
      JOptionPane.showInputDialog(null, "SORRY SOMETHING WENT WRONG PLEASE PRESS ENTER TO CONTINUE"); 

     } 
     try{ 
      //ask for player team name, read team name 
      id3 = JOptionPane.showInputDialog(null, "Enter a players team name"); 
      JOptionPane.showInputDialog("The players team name is " + id3 + ", press enter to continue"); 
      assert id.length()== TEAM_NAME; 
      store.writeUTF(id3); 
     } 
     catch (Exception e){ 
      JOptionPane.showInputDialog(null, "SOMETHING WENT WRONG PLEASE PRESS ENTER TO CONTINUE"); 

     } 

     //enter player skill level, read skill level(0-99) 
     try{  
      id4 = JOptionPane.showInputDialog(null,"Enter a players skill level (0-99)"); 
      recLocation = Integer.parseInt(id4); 
      JOptionPane.showInputDialog("The players skill level " + id4 + " press enter to continue"); 
     } 
     catch (Exception e){ 
      JOptionPane.showInputDialog(null, "SORRY THIS IS NOT AN INTEGER PLEASE PRESS ENTER TO CONTINUE"); 

     } 
     //enter player skill level, read skill level 
     try{  
      id = JOptionPane.showInputDialog(null, "Enter todays Date"); 
      JOptionPane.showInputDialog("Today is " + id + " press enter to continue"); 
      assert id.length()== DRAFT_DATE; 
      store.writeUTF(id); 
     } 
     catch (Exception e){ 
      JOptionPane.showInputDialog(null, "SORRY THIS IS NOT AN INTEGER PLEASE PRESS ENTER TO CONTINUE"); 
      continue; 
     } 
     //convert ID and skill level to string(char-5) 


    } 



    //if command is old, ask for ID and read Id, then use ID to retrieve record, display the record formatted for readability 


    if (cmd.compareToIgnoreCase("old") == 0) { 
     try{ 
      where = JOptionPane.showInputDialog(null, "Enter player:"); 
      recLocation = Integer.parseInt(where); 
      store.seek((PLAYER_ID) * (recLocation-1)); 
      Description = store.readUTF(); 

      JOptionPane.showMessageDialog(null, Description); 
     } 
     catch(Exception e){ 
      JOptionPane.showInputDialog("Sorry there is no player try again"); 
      continue; 
     } 

    if (cmd.compareToIgnoreCase("exit")== 0){ 
      store.close(); 
      System.exit(0); 
      } 
     } 
    } 
} 

希望這有助於!

+0

試過了,但沒有改變任何東西。我相信我需要改變這段時間的條件和/或如果,但不知道要改變的是什麼。 – Chief