简体   繁体   English

当我尝试关闭程序时。 如何解决这个无限循环?

[英]Looping program when I try to close it. How do I fix this infinite loop?

This program is supposed to take in input from the user, put it into a text file, and then be able to pull info if needed later on (not yet implemented). 该程序应该从用户处接收输入,将其放入文本文件,然后能够在以后需要时提取信息(尚未实现)。 The program open and take in values and save them, but when I go to close the program, a new window appears, and this repeats. 程序打开并接受值并保存,但是当我关闭程序时,将出现一个新窗口,并重复进行。 Any help on how to stop this? 对如何阻止这种情况有帮助吗? It might just be a break missing somewhere, but I can't fix it for the life of me. 可能只是某个地方的休息而已,但我无法终生解决。

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;
            }
        }
    }
}

You are only reading input for cmd one time, and then it is never re-assigned. 您只读取一次cmd输入,然后再也不会重新分配它。 So, that first while loop is going to loop forever if its condition ( cmd.compareToIgnoreCase("end")!= 0 ) is true in the first place. 因此,如果第一个while循环的条件( cmd.compareToIgnoreCase("end")!= 0 )首先为true,则它将永远循环。

Your while loop is the culprit. 您的while loop是罪魁祸首。 The loop literally keeps going because there is nothing that is changing the condition. 循环实际上一直在进行,因为没有任何东西可以改变条件。

if you were to make a way to change this condition (cmd.compareToIgnoreCase("end")!= 0) to true somewhere within your script, your script will terminate the way you wanted it to! 如果您想办法将此条件(cmd.compareToIgnoreCase("end")!= 0)更改为true,那么脚本将终止您希望的方式!

NOTE: you are not assigning the word "exit" to a command! 注意:您没有将单词"exit"分配给命令! Maybe making a command for that will make the script do what you want! 也许为此执行命令将使脚本执行您想要的操作!

EDIT: Try changing "end" to "exit" within these snippets here! 编辑:尝试在这些摘要中将"end"更改为"exit"

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

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

EDIT2 : By adding the above if statement to the bottom of your while loop, it should exit! 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);
            }
        }
    }
}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM