为了账号安全,请及时绑定邮箱和手机立即绑定

在Switch菜单上使用do-while循环

在Switch菜单上使用do-while循环

烙印99 2019-04-19 19:15:22
如果在使用do while循环提示“请输入您的模块选择”时选择除1 - 11之外的任何字符,我正在尝试使程序返回菜单列表...目前,即使用户没有选择有效选项,程序也会继续运行我希望在“请选择一个有效的模块”之后返回菜单列表。Scanner scanner = new Scanner(System.in);public void moduleSelection() {     System.out.println("1\t Algorithms");     System.out.println("2\t Advanced Programming");     System.out.println("3\t Computer Architecture and Operating Systems");     System.out.println("4\t Artificial intelligence and Machine Learning");     System.out.println("5\t Computer and Mobile Networks");     System.out.println("6\t Software Engineering");     System.out.println("7\t Big Data Analyics");     System.out.println("8\t Cyber Security Threats");     System.out.println("9\t Research Methods");     System.out.println("10\t Research Project Proposal");     System.out.println("11\t Individual Research Project");     System.out.println("Please entire your Module choice");     int choice;     choice = scanner.nextInt();     switch (choice)     {     case 1: System.out.println("Algorithms");     break;     case 2: System.out.println("Advanced Programming");     break;     case 3: System.out.println("Computer Architecture and Operating Systems");     break;     case 4: System.out.println("Artificial intelligence and Machine Learning");     break;     case 5: System.out.println("Computer and Mobile Networks");     break;     case 6: System.out.println("Software Engineering");     break;     case 7: System.out.println("Big Data Analytics");     break;     case 8: System.out.println("Cyber Security Threats");     break;     case 9: System.out.println("Research Methods");     break;     case 10: System.out.println("Research Project Proposal");     break;     case 11: System.out.println("Individual Research Project");     break;     default: System.out.println("Please select a valid Module");     break;     }}
查看完整描述

7 回答

?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

一个。定义变量以验证用户是否输入了validNumber。湾 在do while循环中,检查数字是否有效


例如:


do{

System.out.println("Please entire your Module choice");


int choice;


choice = scanner.nextInt();

boolean invalidChoice=false;

switch (choice){

case 1:

...

break;

case 2:

...

break;


default: 

invalidChoice=true;

print ("Please enter valid choice");


} while(invalidChoice);


查看完整回答
反对 回复 2019-05-15
?
慕的地10843

TA贡献1785条经验 获得超8个赞

public void moduleSelection() {


boolean switch;


do{


switch = false;


System.out.println("1\t Algorithms");

System.out.println("2\t Advanced Programming");

System.out.println("3\t Computer Architecture and Operating Systems");

System.out.println("4\t Artificial intelligence and Machine Learning");

System.out.println("5\t Computer and Mobile Networks");

System.out.println("6\t Software Engineering");

System.out.println("7\t Big Data Analyics");

System.out.println("8\t Cyber Security Threats");

System.out.println("9\t Research Methods");

System.out.println("10\t Research Project Proposal");

System.out.println("11\t Individual Research Project");



System.out.println("Please entire your Module choice");


int choice;


choice = scanner.nextInt();


switch (choice)

{

case 1: System.out.println("Algorithms");

break;

case 2: System.out.println("Advanced Programming");

break;

case 3: System.out.println("Computer Architecture and Operating Systems");

break;

case 4: System.out.println("Artificial intelligence and Machine Learning");

break;

case 5: System.out.println("Computer and Mobile Networks");

break;

case 6: System.out.println("Software Engineering");

break;

case 7: System.out.println("Big Data Analytics");

break;

case 8: System.out.println("Cyber Security Threats");

break;

case 9: System.out.println("Research Methods");

break;

case 10: System.out.println("Research Project Proposal");

break;

case 11: System.out.println("Individual Research Project");

break;

default: System.out.println("Please select a valid Module");

switch = true;

break;

}

}while(switch)

}


查看完整回答
反对 回复 2019-05-15
?
慕标5832272

TA贡献1966条经验 获得超4个赞

您可以使用标签:


    loop:

    while (true) {

        System.out.println("Please entire your Module choice");


        int choice;


        choice = scanner.nextInt();


        switch (choice) {

            case 1:

                System.out.println("Algorithms");

                break loop;

            case 2:

                System.out.println("Advanced Programming");

                break loop;

            case 3:

                System.out.println("Computer Architecture and Operating Systems");

                break loop;

            case 4:

                System.out.println("Artificial intelligence and Machine Learning");

                break loop;

            case 5:

                System.out.println("Computer and Mobile Networks");

                break loop;

            case 6:

                System.out.println("Software Engineering");

                break loop;

            case 7:

                System.out.println("Big Data Analytics");

                break loop;

            case 8:

                System.out.println("Cyber Security Threats");

                break loop;

            case 9:

                System.out.println("Research Methods");

                break loop;

            case 10:

                System.out.println("Research Project Proposal");

                break loop;

            case 11:

                System.out.println("Individual Research Project");

                break loop;

        }

    }

}


查看完整回答
反对 回复 2019-05-15
?
杨魅力

TA贡献1811条经验 获得超6个赞

根据我作为评论的一部分添加的假设,最好为用户添加帮助文本,例如“请整个模块选择(1-11)”


int choice=0;

boolean isChoiceValid ;


public void moduleSelection() {

while(!isChoiceValid) {

System.out.println("1\t Algorithms");

System.out.println("2\t Advanced Programming");

System.out.println("3\t Computer Architecture and Operating Systems");

System.out.println("4\t Artificial intelligence and Machine Learning");

System.out.println("5\t Computer and Mobile Networks");

System.out.println("6\t Software Engineering");

System.out.println("7\t Big Data Analyics");

System.out.println("8\t Cyber Security Threats");

System.out.println("9\t Research Methods");

System.out.println("10\t Research Project Proposal");

System.out.println("11\t Individual Research Project");

System.out.println("Please entire your Module choice (1-11)");

choice = scanner.nextInt();

if(choice>=1 && choice<=11)

{

isChoiceValid =true;

}

}


查看完整回答
反对 回复 2019-05-15
  • 7 回答
  • 0 关注
  • 804 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信