Java循环仪表转换程序

时间:2019-03-07 08:46:51

标签: java

我无法弄清楚如何使程序在“转换菜单”中循环,然后从示例“ 4 Return”中返回到主菜单。


程序输出示例如下:

主菜单

  1. 输入距离

  2. 退出程序

  

请输入您的选择:1   

     

以米为单位输入距离:500   

转换菜单

  1. 转换为公里

  2. 转换为英寸

  3. 转换为英尺

  4. 返回

  

输入您的选择:1

     

500米为0.5公里

转换菜单

  1. 转换为公里

  2. 转换为英寸

  3. 转换为英尺

  4. 返回

  

输入您的选择:3

     

500米为1640.5英尺   

转换菜单

  1. 转换为公里

  2. 转换为英寸

  3. 转换为英尺

  4. 返回

  

输入您的选择:4   

主菜单

  1. 输入距离

  2. 退出程序

  

请输入您的选择:2   

     

再见!


当程序不包含主菜单并循环返回时,我已经找到了该程序的多种解决方案,但是我似乎找不到任何解决方案。

这就是我所拥有的:

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int choice;
int option;
double meters = 0;
conversionControl();
choice = keyboard.nextInt();    
    switch (choice) {
        case 1: 
            System.out.println("\nEnter a Distance in Meters:"); 
            meters = keyboard.nextDouble();
            break;
        case 2:
            quitProgram();
            break;
        default:
            showError("Please Enter a Valid Option");
            conversionControl();
            option = keyboard.nextInt();
                if (option == 1) {
                    System.out.println("\nEnter a Distance in Meters:"); 
                    meters = keyboard.nextDouble();
                }
                else if ( option == 2) {
                    quitProgram();
                }
            break;
    }
    do{ 
        menu();
        choice = keyboard.nextInt();
        switch (choice) {
            case 1: 
                showKilometers(meters);
                break;
            case 2: 
                showInches(meters);
                break;
            case 3: 
                showFeet(meters);
                break;
            case 4: 
                conversionControl();
                option = keyboard.nextInt();
                if (option == 1) {
                    System.out.println("\nEnter a Distance in Meters:"); 
                    meters = keyboard.nextDouble();
                }
                else if ( option == 2) {
                    quitProgram();
                }
                break;
            default:
                showError("Please Enter a Valid Option");
                menu();
                choice = keyboard.nextInt();
                break;
        }
    } while(choice != 0); {
    }
}

我想我确实找到了自己的出路,但我一直认为这不是正确的方法,或者有更简单的方法。另外,在测试某些输入时会发生一些错误(主要是showError方法调用将输出错误的菜单,或者在输入了太多错误后只会关闭程序)。

任何帮助/建设性的批评将不胜感激。我对编码(了解HTML)还是陌生的,对这个网站也是陌生的。

谢谢! 鲍勃

3 个答案:

答案 0 :(得分:1)

看起来像一个练习,我不会为您提供完整的代码,但是会为您提供一个伪代码来帮助您了解此处的策略。

为澄清起见,我将第一个菜单命名为mainMenu,第二个菜单命名为convMenu

您已经为convMenu实施了良好的策略。这个想法是要形成一个循环,并且仅在用户告诉您时才存在。您缺少的是对mainMenu做同样的事情,并认为convMenumainMenu的子菜单。这意味着当您在convMenu中时,您不在mainMenu之外。

//That's pseudo code
do {
    displayMainMenu();
    readUserInput();
    switch(userInput) {
        case 1 : 
            //here you put your convMenu
            do {
                displayConvMenu();
                readUserInput();
                switch(userInput) {
                    case 1, 2, 3 :
                       doConvertion();
                    case 4 :
                        exitConvMenu = true;
                    default :
                        //wrong input display a message and loop
                }
            } while(!exitConvMenu)
        case 2:
            exitMainMenu = true;
        default :
            //wrong input display a message and loop
    }
} while(!exitMainMenu) 

答案 1 :(得分:0)

一小段地分解代码通常可以更容易理解和调试。分而治之!这是我的处理方式:

首先创建一个代表菜单的Menu类,它可以在屏幕上打印菜单并检查选择是否有效:

class Menu {
    private String title;
    private List<String> choices;
    private String prompt;

    public Menu(String title, List<String> choices, String prompt) {
        this.title = title;
        this.choices = choices;
        this.prompt = prompt;
    }

    public boolean isChoiceAllowed(int i) {
        return i > 0 && i <= choices.size();
    }

    public void show() {
        System.out.println(title);
        for(int i=0; i<choices.size(); i++) {
            System.out.println("  " + (i+1) + ". " + choices.get(i));
        }
        System.out.println(prompt);
    }
}

然后定义您的2个菜单:

private static final Menu mainMenu = new Menu(
    "Main Menu",
    Arrays.asList("Enter Distance", "Quit the program"),
    "Please enter your choice");

private static final Menu conversionMenu = new Menu(
    "Conversion Menu",
    Arrays.asList("Convert to kilometers", "Convert to inches", "Convert to feet", "Return"),
    "Please enter your choice");

有一种读取用户输入的方法:

private static int readUserInput() {
    int input = keyboard.nextInt();
    // Check if input is valid, if not call back readUserInput() until a valid input is given
    if(!currentMenu.isChoiceAllowed(input)) {
        System.out.println("Please Enter a valid option");
        return readUserInput();
    }
    return input;
}

在主菜单中有一种处理选择的方法

private static void handleMainMenuChoice(int choice) {
    switch (choice) {
        case 1:
            System.out.println("\nEnter a Distance in Meters:");
            meters = keyboard.nextDouble();
            // Set the current menu to be the Conversion Menu
            currentMenu = conversionMenu;
            break;
        case 2:
            quitProgram();
            break;
    }
}

在“转换菜单”中有一种处理选择的方法

private static void handleConversionMenuChoice(int choice) {
    switch (choice) {
        case 1:
            showKilometers(meters);
            break;
        case 2:
            showInches(meters);
            break;
        case 3:
            showFeet(meters);
            break;
        case 4:
            // Set the current menu to be the Main Menu
            currentMenu = mainMenu;
            break;
    }
}

有一种方法可以处理用户的选择并将其分配给上面的适当方法:

private static void handleChoice(int choice) {
    if(currentMenu == mainMenu) {
        handleMainMenuChoice(choice);
    } else {
        handleConversionMenuChoice(choice);
    }
}

然后将其绑在一起:

private static double meters;
private static Menu currentMenu;
private static Scanner keyboard = new Scanner(System.in);

<Insert menu definitions here>

public static void main(String[] args) {

    currentMenu = mainMenu;

    int choice;
    while (true) {
        // Show the current menu
        currentMenu.show();
        // Read the user input
        choice = readUserInput();
        // Handle the user choice
        handleChoice(choice);
    }
}

答案 2 :(得分:0)

public static void main(String[] args) {
        int choice = 0;
        int option;
        double meters = 0;
        Scanner keyboard = new Scanner(System.in);
        boolean exit = false;
        while(!exit) {

            System.out.println("\nEnter your choice :Enter Distance.. click1\r\n" + 
                    "\r\n" + 
                    "Quit the program.. click2"); 
            choice = keyboard.nextInt();    
            switch (choice) {
                case 1: 
                    System.out.println("\nEnter a Distance in Meters:"); 
                    meters = keyboard.nextDouble();
                    System.out.println("Conversion Menu\r\n" + 
                            "\r\n" + 
                            "Convert to kilometers .. click1\r\n" + 
                            "\r\n" + 
                            "Convert to inches .. click2\r\n" + 
                            "\r\n" + 
                            "Convert to feet .. click3\r\n" + 
                            "\r\n" + 
                            "Return.... .. click4"); 
                    int choice1 = keyboard.nextInt();
                    switch(choice1) {
                        case 1:
                            showKilometers(meters);
                            break;
                        case 2:
                            showInches(meters);
                            break;
                        case 3:
                            showFeet(meters);
                            break;
                        case 4:
                            exit = true;
                            break;
                    }
                    break;
                case 2:
                    exit = true;
                    break;
                default:
                    break;
            }
        }
    }