我在这里有主类的循环
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");
}
该方法正确输出所有内容,但在控制台中您必须输入一些内容才能继续循环。 所以我的问题是如何在不先输入内容的情况下再次开始循环? IE。在方法打印其内容后直接打印“等待新操作”
最佳答案
改变
while (!input.next().equals("exit"));
至
while (!command_string.equals("exit"));
关于java - 在调用循环中的方法后如何继续循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36750296/