如何在Java中创建循环菜单?

时间:2017-10-04 00:24:41

标签: java

我是Java和学习的新手。我正在用Java创建一个循环菜单。但是,当我选择“a”并输入详细信息时,它不会返回菜单。

我做了一些研究,我需要在这里添加一个Do和While循环,但我对如何在这里实现它感到困惑。非常感谢一点指导

下面是我的代码:

switch(selection) {

case 'A':
case 'a':
    System.out.print("Enter a Trip Date: ");
    date = input.nextLine();

    System.out.print("Enter Trip Point: ");
    enter_point = input.nextLine();

    System.out.print("Enter Exit Point: ");
    exit_point = input.nextLine();


    break;

case 'B':
case 'b':
    System.out.print("Enter Breakdown Date: ");
    breakdown = input.nextLine();

    System.out.print("Enter Sector Breakdown Occured in: ");
    sector_break = input.nextInt();

    System.out.print("Enter Vehicle Recovery Cost: ");
    rec_cost = input.nextDouble();

    break;

case 'x':
case 'X':
    System.out.println("Exiting data entry menu..");
    break;

//equivalent to an else
default:
    System.out.println("ERROR! - Please Enter a Valid Selection!");




}   
    while(selection != 'X');

2 个答案:

答案 0 :(得分:0)

这是你需要的吗?

char selection;
do
{
    do
    {
        //Menu
        System.out.println("Toll Road Data Entry Menu");
        System.out.println("-----------------------------------------");

        System.out.println("A - Record Trip");
        System.out.println("B - Record Breakdown Incident");
        System.out.println("X - Exit");

        System.out.print("Enter Your Selection: ");
        selection = input.nextChar(); 
        if (selection!='a' || selection!='A' || selection!='b' || selection!='B' || selection!='x' || selection!='X') 
        { 
            System.out.println("Selection must be a single character, A,B or X");
            continue;
        }
        else
            break;
    } while (1);

    switch(selection)
    {
        case 'A':
        case 'a':
            System.out.print("Enter a Trip Date: ");
            date = input.nextLine();

            System.out.print("Enter Trip Point: ");
            enter_point = input.nextLine();

            System.out.print("Enter Exit Point: ");
            exit_point = input.nextLine();
            break;
        case 'B':
        case 'b':
            System.out.print("Enter Breakdown Date: ");
            breakdown = input.nextLine();

            System.out.print("Enter Sector Breakdown Occured in: ");
            sector_break = input.nextInt();

            System.out.print("Enter Vehicle Recovery Cost: ");
            rec_cost = input.nextDouble();
            break;
        case 'x':
        case 'X':
            System.out.println("Exiting data entry menu..");
            break;
        //equivalent to an else
        default:
            System.out.println("ERROR! - Please Enter a Valid Selection!");
    }   
} while(selection != 'X');

答案 1 :(得分:0)

我通常在while循环中通过它们,这样只需通过我完成后返回主菜单

public static void main(String[] args)  {

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    Scanner input = new Scanner(System.in);

    boolean menu=true;

    int selection,sector_break;

    String date,enter_point,exit_point,
            breakdown;

    double rec_cost;

    while(true)
    {
        System.out.print(String.format("\033[2J"));

        System.out.print("Your Menu Title \n\n");
        System.out.print("1. Selection A\n");
        System.out.print("2. Selection B\n");
        System.out.print("3. Selection C\n");
        System.out.print("4. Selection D\n");
        System.out.print("5. Exit Menu D\n");
        selection = Integer.parseInt(input.nextLine());

        if(selection<1 || selection>4)
            return;


        switch(selection){
            case 1:
                System.out.print("Enter a Trip Date: ");
                date = input.nextLine();

                System.out.print("Enter Trip Point: ");
                enter_point = input.nextLine();

                System.out.print("Enter Exit Point: ");
                exit_point = input.nextLine();
                break;

            case 2:
                System.out.print("Enter Breakdown Date: ");
                breakdown = input.nextLine();

                System.out.print("Enter Sector Breakdown Occured in: ");
                sector_break = input.nextInt();

                System.out.print("Enter Vehicle Recovery Cost: ");
                rec_cost = input.nextDouble();

                break;                    
            case 3:
                // your code here
            default:
                // your code here
        }
    } 
}
}