Java 调用循环中的方法后,如何继续循环?

Java 调用循环中的方法后,如何继续循环?,java,Java,我有一个来自主类的循环 do { System.out.println("Awaiting new action"); command_string = input.next(); main.Commands(command_string); } while (!input.next().equals("exit")); 然后,输入的用户字符串通过名为Commands的方法中的一个单独循环运行。此处显示 public void C

我有一个来自主类的循环

do {

     System.out.println("Awaiting new action");
     command_string = input.next();
     main.Commands(command_string);

       }
    while (!input.next().equals("exit"));
然后,输入的用户字符串通过名为Commands的方法中的一个单独循环运行。此处显示

    public void Commands(String commandinput)
{
 if (commandinput.equals("inv") || 
     commandinput.equals("help")||
     commandinput.equals("exit"))
 {
   if (commandinput.equals("inv"))
   {
      System.out.println("You have a total of " + mainsave.totalitems + " items");
      inventoryGrid();
   }
    if (commandinput.equals("help"))
   {
      System.out.println("[[[COMMAND LIST]]]\n" +
                         "Help -> Opens this menu\n" + 
                         "Inv  -> Views your current inventory\n" +
                         "Exit -> Exits the application\n");

   }
   if (commandinput.equals("exit"))
   {
        System.out.println("Exiting...");
        System.exit(0);
   }
 }
 else
    {
        System.out.println("Invalid command. Use the command 'help' to access command list");
    }
该方法正确地输出所有内容,但在控制台中,您必须输入一些内容才能继续循环。 所以我的问题是如何在不先键入内容的情况下再次启动循环? 即,在方法打印其内容更改后直接打印“等待新操作”

while (!input.next().equals("exit"));

改变

while (!input.next().equals("exit"));


试试这个。当用户在循环的第一次迭代中输入
“exit”
时,这将退出一次

do {
    System.out.println("Awaiting new action");
    command_string = input.nextLine();

      //break the loop even user enter "exit" in first Iteration.
    if(command_string.equals("exit")) 
        break;            
    main.Commands(command_string);
} while (true);

试试这个。当用户在循环的第一次迭代中输入
“exit”
时,这将退出一次

do {
    System.out.println("Awaiting new action");
    command_string = input.nextLine();

      //break the loop even user enter "exit" in first Iteration.
    if(command_string.equals("exit")) 
        break;            
    main.Commands(command_string);
} while (true);
input.next()
替换为
input.nextLine
,不要调用它两次,因为您需要键入两次:)而是使用已定义的字符串变量
命令\u string

例子: 将
input.next()
替换为
input.nextLine
,不要调用它两次,因为您需要键入两次:)而是使用已定义的字符串变量
命令\u string

例子:
事实上不是这样真的吗?在我看来,他在while条件下再次调用scanners next()方法,这当然会等待新的输入,而不是使用已输入的输入。如果用户第一次输入“exit”,则您的方法将至少调用一次方法
main.Commands(command_string)
是,是,我的意思是exit命令调用System.exit,并且不会达到while条件,所以它可以被while(true)替换,这是有效的!非常感谢。我是新来的,反应时间令人难以置信。事实并非如此真的吗?在我看来,他在while条件下再次调用scanners next()方法,这当然会等待新的输入,而不是使用已输入的输入。如果用户第一次输入“exit”,则您的方法将至少调用一次方法
main.Commands(command_string)
是,是,我的意思是exit命令调用System.exit,并且不会达到while条件,所以它可以被while(true)替换,这是有效的!非常感谢。我是新来的,响应时间令人难以置信。@srv_sud这里有一个
do-while
。@srv_sud你是对的,但是这个问题从来没有使用过标准的while循环,所以我不知道你为什么建议使用do-while循环used@srv_sud有一个
do while
。@srv\u如果你是对的,但是这个问题从来没有使用过标准的while循环,所以我不知道你为什么建议使用do-while,因为它已经被使用了