【问题标题】:Repeating a void method重复 void 方法
【发布时间】:2014-01-29 14:31:52
【问题描述】:
    public static void main(String[] args)
{
    CollegeTester ct = new CollegeTester();
    ct.getCommand();//goes to command
}   

//Ask user for a command
public void getCommand()
{
    String command = "";
    System.out.println("Enter a command: ");
    command = input.nextLine();
        if(command.equals("add"))
            addCommand();//If command is add go to addCommand
        if(command.equals("course"))
            courseCommand();//If command is course go to courseCommand
        if(command.equals("find"))
            findCommand();
        if(command.equals("remove"))
            removeCommand();
        if(command.equals("highest"))
            highestCommand();
        if(command.equals("login"))
            loginCommand();
        if(command.equals("quit"))
            System.exit(1);
}

我想重复 getCommand() 方法,直到用户输入退出但总是失败

  1. 我尝试将 getCommand() 放在其他方法的末尾,如果语句不会重复

  2. 我尝试对所有内容进行 while 循环,但如果有人不小心输入错误退出程序,程序将永远不会结束

在用户希望退出之前,我将如何有效地返回此方法?

【问题讨论】:

  • 正确使用do/while。你能用它来展示你的实现吗?
  • 执行命令!= 退出?如果我一旦进入无限循环就按退格键
  • 不要使用==比较字符串值。使用equals()
  • 同样的事情会发生 { stuff }while(!(command.equals("quit")); 如果你按退格键字符停留在java中会发生什么,所以你永远不能退出程序.

标签: java if-statement methods repeat


【解决方案1】:

我会选择两种方式之一。

public static void main(String[] args)
{
    CollegeTester ct = new CollegeTester();
    ct.getCommand();//goes to command

}  

//Ask user for a command
public void getCommand()
{
    boolean exit = false;
    while(!exit){
        String command = "";
        System.out.println("Enter a command: ");
        command = input.nextLine();
            if(command.equals("add")){
                addCommand();//If command is add go to addCommand
            }else if(command.equals("course")){
                courseCommand();//If command is course go to courseCommand
            }else if(command.equals("find")){
                findCommand();
            }else if(command.equals("remove")){
                removeCommand();
            }else if(command.equals("highest")){
                highestCommand();
            }else if(command.equals("login")){
                loginCommand();
            }else if(command.equals("quit")){
                exit = true;
            }else {
                System.out.println("Not valid command, try again.");
            }
    }

}



//Ask user for a command
public void getCommand()
{
    while(true){
        String command = "";
        System.out.println("Enter a command: ");
        command = input.nextLine();
            if(command.equals("add"))
                addCommand();//If command is add go to addCommand
            if(command.equals("course"))
                courseCommand();//If command is course go to courseCommand
            if(command.equals("find"))
                findCommand();
            if(command.equals("remove"))
                removeCommand();
            if(command.equals("highest"))
                highestCommand();
            if(command.equals("login"))
                loginCommand();
            if(command.equals("quit"))
                System.exit(0);  // or could use 'break' here (thanks Mikkel)
    }

}

编辑:更正 System.exit(1)。谢谢米克尔。

【讨论】:

  • 或者,您也可以在 while(true) 循环之外只使用 breakSystem.exit() 也应该用 0 调用,表示干净的退出。
【解决方案2】:

或者你可以把它放在一个while循环中:

while(true) {
    getCommand();
}

另外,查看this question on System.exit(int),您实际上是在说应用程序应该因为错误而关闭。当它打算关闭时,你应该传递0,而不是1

事实上,如果你使用像上面这样的while循环,我可能只是通过return false;或其他什么东西来逃避循环,让程序自然结束。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-04
    • 2010-11-16
    • 1970-01-01
    • 2014-01-05
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多