【问题标题】:Having trouble with looping a Switch case and nested while loop循环 Switch 案例和嵌套 while 循环时遇到问题
【发布时间】:2014-10-28 21:51:01
【问题描述】:

我对编程很陌生,因为这是我在大学的第一个学期,没有任何先验知识。在用 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();
    }
}

【问题讨论】:

  • 为什么需要嵌套的while循环?
  • 这是作业的一部分。但我不明白把它放在哪里,以便用户可以在需要时再次运行开关。 @Moh123
  • 您的作业可能是由不太擅长编程的人设置的。恕我直言,您应该使用 for 循环,而不是 while 循环 - 因为您有一个迭代方面(重新询问用户是否输入无效响应)。此外,作业不应规定实施 - 选择一个是学习的一部分。

标签: java while-loop switch-statement


【解决方案1】:

一点概念上的帮助:

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

哪些部分需要重复?你(或者更确切地说,你的导师)可能期待的行为是,在算命之后,“你想让我算命吗”这个问题会再次出现。这意味着您必须将它以及它所包含的所有内容(算命本身)放在一个循环中。

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

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

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

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

【讨论】:

  • 这实际上帮助了我很多!我会尝试解决这个问题。我真的不想直接回答,所以我可以自己弄清楚。下次我回复希望它会得到纠正。再次感谢您!
【解决方案2】:

试试下面的。你不需要 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 块中,以确保您将捕获使用扫描仪时可能发生的任何异常。如果您使用 java 1.7 及更高版本,请在 finally 块中关闭扫描仪,甚至更好地使用 try-with-resources。

【讨论】:

    【解决方案3】:

    您不需要嵌套循环。 While 循环可以很好地完成工作。你也只需要一个 Var0 这也是保持/停止循环的条件。整个代码都在 try-catch 块中,以解决当用户键入非 int 的内容时的问题。 finally 块在最后关闭扫描仪 - 无论是否有异常。

            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();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-13
      • 2016-06-06
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 2017-06-02
      • 2020-11-02
      • 1970-01-01
      相关资源
      最近更新 更多