循环Switch案例和嵌套while循环时遇到问题

时间:2014-10-28 21:51:01

标签: java while-loop switch-statement

我对编程很陌生,因为这是我大学的第一个学期,没有先验知识。在使用Python之后,现在使用Java工作,我们正在开发一个算命计划。我遇到的主要问题是试图回到开关询问用户是否想再次播放,或者他们是否输入了8个案例之外的无效回复。还必须有一个while循环嵌套在另一个while循环中。

Scanner user = new Scanner(System.in);
    System.out.println("Welcome to the Fortune Telling program.\n"); //Welcome message

    System.out.print("Would you like me to tell your fortune? Type 1 for yes and any other number for no: "); //ask for yes or no to run
    int Var0 = user.nextInt();
    if (Var0 == 1)
    {
        System.out.print("Enter a number 1-8 and I will tell your fortune: "); //ask for number between 1-8 to find fortune or invalid
        int Var1 = user.nextInt();                                              
            switch (Var1) 
            {
            case 1:                                                                             //case 1-8 fortunes
                System.out.println("\nYou will become great if you believe in yourself.");
                break;
            case 2:
                System.out.println("\nSerious trouble with bypass you.");
                break;
            case 3:
                System.out.println("\nYou will travel to many exotic places in your lifetime.");
                break;
            case 4:
                System.out.println("\nYour ability for accomplishment will follow with success.");
                break;
            case 5:
                System.out.println("\nWhen fear hurts you, conquer it and defeat it!");
                break;
            case 6:
                System.out.println("\nYou will be called in to fulfill a position of higher honor and responsibility.");
                break;
            case 7:
                System.out.println("\nYour golden opportunity is coming shortly.");
                break;
            case 8:
                System.out.println("\nIntegrity is doing the right thing, even when nobody is watching.");
                break;
            default:
                System.out.print("That's not a valid number. Try again.\n");                         //invalid number try to rerun for correct response
            }
        }                                                                                            //display next print only on case not default
    System.out.print("Would you like another fortune? Type 1 for yes and any other number for no: "); //loop this back into 'switch'
    int Var2= user.nextInt();

    System.out.print("Thank you for trying the fortune telling program.");                           //Thank you message
    user.close();
    }
}

3 个答案:

答案 0 :(得分:0)

您不需要嵌套循环。虽然循环工作正常。您还需要一个Var0,这也是保持/停止循环的条件。当用户输入非int的东西时,整个代码在try-catch块内部来解决问题。最后阻止扫描仪结束 - 无论是否有例外。

        Scanner user = new Scanner(System.in);
        try {

            System.out.println("Welcome to the Fortune Telling program.\n");
            System.out
                    .print("Would you like me to tell your fortune? Type 1 for yes and any other number for no: ");
            int Var0 = user.nextInt();
            while (Var0 == 1) {
                System.out
                        .print("Enter a number 1-8 and I will tell your fortune: ");
                int Var1 = user.nextInt();
                switch (Var1) {
                case 1: // case 1-8 fortunes
                    System.out
                            .println("\nYou will become great if you believe in yourself.");
                    break;
                case 2:
                    System.out.println("\nSerious trouble with bypass you.");
                    break;
                case 3:
                    System.out
                            .println("\nYou will travel to many exotic places in your lifetime.");
                    break;
                case 4:
                    System.out
                            .println("\nYour ability for accomplishment will follow with success.");
                    break;
                case 5:
                    System.out
                            .println("\nWhen fear hurts you, conquer it and defeat it!");
                    break;
                case 6:
                    System.out
                            .println("\nYou will be called in to fulfill a position of higher honor and responsibility.");
                    break;
                case 7:
                    System.out
                            .println("\nYour golden opportunity is coming shortly.");
                    break;
                case 8:
                    System.out
                            .println("\nIntegrity is doing the right thing, even when nobody is watching.");
                    break;
                default:
                    System.out.print("That's not a valid number. Try again.\n");
                }
                System.out
                        .print("Would you like another fortune? Type 1 for yes and any other number for no: ");
                Var0 = user.nextInt();
            }

            System.out
                    .print("Thank you for trying the fortune telling program.");
        } catch (Exception e) {
System.out.println("This is what you tell if user types something which is not a digit");
        } finally {
            user.close();
        }

答案 1 :(得分:0)

一些概念上的帮助:

当你说“我需要返回开关......”时,这意味着你需要一个循环。

需要重复的部分是什么?你(或者更确切地说,你的教练)可能期望的行为是,在告诉财富之后,“你想让我告诉你的财富”这个问题会再次出现。这意味着你必须把它和它所需的一切(算命本身)放在一个循环中。

在这种情况下通常的构造是

  • 显示问题
  • 获取用户输入
  • 循环,条件是用户未输入“完成”输入
    • 执行用户输入所需的任何任务。
    • 再次显示问题
    • 再次获取用户输入,以便下次循环检查条件时,它将具有计算的最新值。

你能想到程序中适合这种模式的部分吗?

现在接下来的事情是你需要一段时间。这是一个提示:程序希望用户输入值1-8。如果他输入'9'或'0'或其他什么,那么程序是否应该忽略这一点并再次询问他是否想要发财,还是应该坚持?

答案 2 :(得分:0)

尝试以下方法。你不需要var1

public static void main(String[] args)
{
    // TODO Auto-generated method stub
    Scanner user = new Scanner(System.in);
    System.out.println("Welcome to the Fortune Telling program.\n"); //Welcome message

    System.out.print("Would you like me to tell your fortune? Type 1 for yes and any other number for no: ");

    int Var0 = 0;

    while(Var0 != -1)
    {
        System.out.print("Enter a number 1-8 and I will tell your fortune or -1 to quit "); //ask for number between 1-8 to find fortune or invalid

        Var0 = user.nextInt();

        switch (Var0) 
        {
        case 1:                                                                             //case 1-8 fortunes
            System.out.println("\nYou will become great if you believe in yourself.");
            break;
        case 2:
            System.out.println("\nSerious trouble with bypass you.");
            break;
        case 3:
            System.out.println("\nYou will travel to many exotic places in your lifetime.");
            break;
        case 4:
            System.out.println("\nYour ability for accomplishment will follow with success.");
            break;
        case 5:
            System.out.println("\nWhen fear hurts you, conquer it and defeat it!");
            break;
        case 6:
            System.out.println("\nYou will be called in to fulfill a position of higher honor and responsibility.");
            break;
        case 7:
            System.out.println("\nYour golden opportunity is coming shortly.");
            break;
        case 8:
            System.out.println("\nIntegrity is doing the right thing, even when nobody is watching.");
            break;
        default:
            System.out.print("That's not a valid number. Try again.\n"); //invalid number try to rerun for correct response
        }

    }
    System.out.print("Thank you for trying the fortune telling program.");//Thank you message
    user.close();
}

将代码包含在try catch块中,只需确保捕获使用扫描程序时可能发生的任何异常。关闭finally块中的扫描程序,如果使用java 1.7及更高版本,请使用try-with-resources更好。