2016-07-13 10 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の入力を1回だけ読み取ってから、再割り当てされることはありません。だから、その最初の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:あなたの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

関連する問題