Использование цикла do-while в меню Switch - PullRequest
0 голосов
/ 14 мая 2019

Я пытаюсь заставить программу вернуться в список меню, если какой-либо символ, кроме 1 - 11, выбран при появлении запроса «Пожалуйста, введите ваш выбор модуля» с помощью цикла do while ...

В настоящее время, даже если пользователь не выбрал допустимую опцию, программа просто продолжает работать

Я ожидаю после "Пожалуйста, выберите действительный модуль", чтобы он вернулся в список меню.

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 ]

0 голосов
/ 14 мая 2019

Вы можете использовать метку:

    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;
        }
    }
}
0 голосов
/ 14 мая 2019
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)
}
0 голосов
/ 14 мая 2019

вы можете изменить свой код как:

int choice;
choice = scanner.nextInt();

if(choice>=1&& choice<=11){
switch (choice)
{ ...
0 голосов
/ 14 мая 2019

Вы можете сделать это, просто добавив while или do-while loop

Обновил ваш метод.Выезд:

private void moduleSelection() {
    int choice = -1;

    while (true) { 
        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("0\t Exit");
        System.out.println("Please entire your Module choice");

        choice = scanner.nextInt();
        switch (choice) {
        case 0:
            System.exit(1);
            break;
        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;
        }
    }
}
0 голосов
/ 14 мая 2019

Вам нужно переключить корпус переключателя в боковой петле.Помимо этого вам необходимо предоставить возможность выхода из цикла.Я использую существующее условие как 99 в качестве ввода.

     public static void main(String[] args) {


         boolean doYouWantToExit = false;
         Scanner scanner = new Scanner(System.in);
         do {
             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("99\t Do you want to exist");
             System.out.println("Please entire your Module choice");
             int choice;
             choice = Integer.parseInt(scanner.nextLine());
             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;
             case 99: 
                 doYouWantToExit = true;
             break;
             default: System.out.println("Please select a valid Module");
             break;
             }

         }while(!doYouWantToExit); 
     }
0 голосов
/ 14 мая 2019

Исходя из моих предположений, добавленных в качестве части комментариев, лучше добавить текст справки для пользователя, например "Пожалуйста, укажите весь свой выбор модуля (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;
}
}
0 голосов
/ 14 мая 2019

а.Определите переменную, чтобы убедиться, что пользователь ввел 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);
...