简体   繁体   English

将Java循环回到代码的开头

[英]Loop java back to the beginning of the code

I am looking for a way to loop a program back to the beginning if the user chooses at the end. 我正在寻找一种方法,如果用户选择在结尾处将程序循环回到开头。

The program is a menu program I have used switch statements all throughout at the end after it prints out the receipt and all other details required have been validated it is meant to ask the user if they want to order again and should loop forever until they choose to exit. 该程序是一个菜单程序,在打印出收据并且所有其他详细信息都经过验证之后,我在整个过程中都使用了switch语句,这是要询问用户是否要再次订购,并且应该永远循环直到他们选择为止退出。

I have tried another switch statement 我尝试了另一个switch语句

    switch (end) {
case 1:
coffee = keyboard.nextInt();
break;
case 2:
tea = keyboard.nextInt();
break;
case 3:
System.out.println("Bye bye\n");
System.exit(0);
break;}

But obviously it will take the next in but will not go back to that part of the program how would you make it do this ? 但是很显然,它将需要下一个,但不会回到程序的那一部分,您将如何做到这一点?

Thanks 谢谢

go for 去做

while(true){
   switch(end)
…..
…..
…..
…..
}

Put the whole relevant section inside a loop; 将整个相关部分放在一个循环中; for this sort of "do you want to continue" situation, usually a do...while loop. 对于这种“您想继续吗”的情况,通常是do...while循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM