By Kevin


2020-03-26 03:02:44 8 Comments

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;

        }
    }
}

2 comments

@MangduYogii 2020-03-26 03:36:56

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');

@Ed B 2020-03-26 03:15:46

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.

Related Questions

Sponsored Content

66 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 3995479 View
  • 3456 Score
  • 66 Answer
  • Tags:   java random integer

28 Answered Questions

44 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

90 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

42 Answered Questions

[SOLVED] How do I efficiently iterate over each entry in a Java Map?

32 Answered Questions

[SOLVED] When to use LinkedList over ArrayList in Java?

27 Answered Questions

[SOLVED] How does the Java 'for each' loop work?

35 Answered Questions

[SOLVED] How do I break out of nested loops in Java?

55 Answered Questions

[SOLVED] How to create a memory leak in Java?

58 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

Sponsored Content