Understanding Java while loops

2020-03-26 java

Hi am new to java programming and I am trying to understand while loops. I haven't used it in the code below but I have an assignment that requires me to. What I want the program to do is simply re prompt the user to enter a menu option 1 to 5 when the code in the switch statement executes based on the user input. I am unsure where to put the while loop in the code and also what to write inside of it. Can someonme please help me with the program I am to create? It also requires me to use a switch statement to evalute the user input. All comments would be appreciated!

import java.util.Scanner;

public class Student_Grade {

    public static void main(String[] args) {
        get_method();
    }

    public static void get_method() {
        int num;

        Scanner menu = new Scanner(System.in);
        System.out.println("Please Enter menu 1 and 5 and 0 to exit");

        switch (num = menu.nextInt()) {
            case 1:
                System.out.println("You entered menu option 1");
                break;

            case 2:
                System.out.println("you entered menu option 2");
                break;

            case 3:
                System.out.println("you entered menu option 3");
                break;

            case 4:
                System.out.println("you entered menu option 4");
                break;

            case 5:
                System.out.println("you entered menu option 3");
                break;

            default:
                System.out.println("You entered an invalid option");
                break;

        }
    }
}

Answers

If I understand your question correctly you will want to put the while loop around the call for the action. In your case you have not specified if one of the options exits or on what condition the program should exit. In that case I have to assume that it loops indefinently. The code could be as below.

import java.util.Scanner;

public class Student_Grade {

    public static void main(String[] args) {
        while(true) {
            get_method();
        }
    }

    public static void get_method() {
        int num;

        Scanner menu = new Scanner(System.in);
        System.out.println("Please Enter menu 1 and 5 and 0 to exit");

        switch (num = menu.nextInt()) {
            case 1:
                System.out.println("You entered menu option 1");
                break;

            case 2:
                System.out.println("you entered menu option 2");
                break;

            case 3:
                System.out.println("you entered menu option 3");
                break;

            case 4:
                System.out.println("you entered menu option 4");
                break;

            case 5:
                System.out.println("you entered menu option 3");
                break;

            default:
                System.out.println("You entered an invalid option");
                break;

        }
    }
}

The addition would be the

while(true) {
    get_method();
}

You could also restructure it to add a continue option. Or to exit on one of the options say option 5 is the exit option than the program could be written as follows:

import java.util.Scanner;

public class Student_Grade {

    public static void main(String[] args) {
         get_method();
    }

    public static void get_method() {
        int num;

        while(num != 5) {
            Scanner menu = new Scanner(System.in);
            System.out.println("Please Enter menu 1 and 5 and 0 to exit");

            switch (num = menu.nextInt()) {
                case 1:
                    System.out.println("You entered menu option 1");
                    break;

                case 2:
                    System.out.println("you entered menu option 2");
                    break;

                case 3:
                    System.out.println("you entered menu option 3");
                    break;

                case 4:
                    System.out.println("you entered menu option 4");
                    break;

                case 5:
                    System.out.println("you entered menu option 3");
                    break;

                default:
                    System.out.println("You entered an invalid option");
                    break;

            }
        }
    }
}

There are a number of different ways to handle loops but this should get you started.

do_While loop is best for menu driven application as per your code structure

  do{
                    System.out.println("Please Enter menu 1 and 5 and 0 to exit");
                    System.out.println(" menu option 1");
                    System.out.println(" menu option 2");
                    System.out.println(" menu option 3");
                    System.out.println("menu option 4);
                    System.out.println(" menu option 5");

         int num= menu.nextInt();


    switch (num) {
                case 1:
                    System.out.println("You entered menu option 1");
                    //you can write while loop here or
                    // call new method which deals with while loop
                    break;

                case 2:
                    System.out.println("you entered menu option 2");
                    break;

                case 3:
                    System.out.println("you entered menu option 3");
                    break;

                case 4:
                    System.out.println("you entered menu option 4");
                    break;

                case 5:
                    System.out.println("you entered menu option 5");
                    break;
               case 0:
                    System.out.println("you entered menu option ");
                    exit(0);
                    break;

                default:
                    System.out.println("You entered an invalid option");
                    break;

            }
                    System.out.println("do you want to continue? Y/N");

        }while(choice!='Y');

Related