我是靠谱客的博主 时尚马里奥,最近开发中收集的这篇文章主要介绍java 中断语句_Java如何在switch语句下中断while循环?,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

如何终止内部菜单?

示例代码:

import java.util.Scanner;

public class Example {

public static void main(String[] args) {

Scanner input = new Scanner(System.in); //used to get input

int option1, option2 = 0;

boolean loop_terminate = true; //flag used to terminate inner while loop

//Main Menu

while (true) {

//Main Menu options

System.out.println("1.Option 1");

System.out.println("2.Option 2");

System.out.println("3.Option 3");

System.out.println("4.Option 4");

System.out.println("5.Exit main menu");

System.out.print("Please enter your choice : ");

option1 = input.nextInt();

switch (option1) {

case 1:

//do something here

break;

case 2:

//do something here

break;

case 3:

while (loop_terminate) {

//Inner menu options

System.out.println("1.Inner Menu option 1");

System.out.println("2.Inner Menu option 2");

System.out.println("3.Inner Menu option 3");

System.out.println("4.Return to Main Menu");

System.out.print("Please enter your choice : ");

option2 = input.nextInt();

switch (option2) {

case 1:

break;

case 2:

break;

case 3:

break;

case 4:

loop_terminate = false; //this will terminate inner menu

break;

default:

System.out.println("Invalid option");

break;

}

}

break; //never forget to add this break statement

case 4:

break;

case 5:

return; //terminate outer menu

default:

System.out.println("Invalid option");

}

}

}

}

最后

以上就是时尚马里奥为你收集整理的java 中断语句_Java如何在switch语句下中断while循环?的全部内容,希望文章能够帮你解决java 中断语句_Java如何在switch语句下中断while循环?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部