因此,正如 Turing85 所提到的,重新启动整个程序可能不是要走的路。通常,您使用所谓的状态机。对于此示例,可以使用 while 循环来实现一个简单的示例。这是一个例子:
import java.util.Scanner;
public class foo
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
boolean running = true;
while(running){
System.out.println("enter a value, enter -1 to exit...");
int value = scan.nextInt();
if(value == -1){
System.out.println("exiting");
break;
}else{
System.out.println("do stuff with the value");
}
}
}
}
这是输出:
enter a value, enter -1 to exit...
1
do stuff with the value
enter a value, enter -1 to exit...
2
do stuff with the value
enter a value, enter -1 to exit...
4
do stuff with the value
enter a value, enter -1 to exit...
-1
exiting