首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我试图关闭它时,循环程序。我如何修复这个无限循环?

当我试图关闭它时,循环程序。我如何修复这个无限循环?
EN

Stack Overflow用户
提问于 2016-07-14 02:26:06
回答 2查看 65关注 0票数 2

这个程序应该接受用户的输入,将其放入一个文本文件中,然后能够在以后需要时提取信息(尚未实现)。程序打开并接受值并保存它们,但当我关闭程序时,会出现一个新窗口,并且重复此过程。有什么关于如何阻止这种情况的帮助吗?它可能只是在某个地方丢失了一个突破口,但我终生无法修复它。

代码语言:javascript
复制
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;
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2016-07-15 05:12:48

您只读取cmd的输入一次,然后它永远不会被重新分配。因此,如果第一个while循环的条件(cmd.compareToIgnoreCase("end")!= 0)一开始就为真,那么它将永远循环。

票数 0
EN

Stack Overflow用户

发布于 2016-07-15 05:24:46

你的while loop是罪魁祸首。从字面上讲,循环一直在进行,因为没有任何东西在改变条件。

如果你想在你的脚本中把这个条件(cmd.compareToIgnoreCase("end")!= 0)改成true,你的脚本就会按照你想要的方式终止!

注意:您没有将单词"exit"分配给命令!也许为它做一个命令会让脚本做你想做的事情!

EDIT:在这里尝试将这些代码片段中的"end"更改为"exit"

代码语言:javascript
复制
if (cmd.compareToIgnoreCase("end")== 0){
        store.close();
        System.exit(0);

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

EDIT2:通过将上面的if语句添加到while循环的底部,它应该退出!

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38359192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档