如何正确格式化我的代码以在Java中运行此菜单程序?

发布于 2025-02-06 04:20:55 字数 1488 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开浅夏 2025-02-13 04:20:55

您的代码有一些问题。我注意到的第一个是您没有声明在默认情况下尝试为值分配值的“选择”变量。

您面临的错误是由于未关闭主要方法,您在关闭WARE循环后只是错过了“}”。

在这里,它是正确关闭的:

    public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    double num1, num2;
    boolean keepGoing = true;
    int choice;
    double answer = 0.0;
    double calcTax;

    System.out.print("Please give me a number: ");
    num1 = kb.nextDouble();
    System.out.print("Please give me another number: ");
    num2 = kb.nextDouble();

    while (keepGoing) {
        System.out.print("Which would you like to do? ");
        choice = kb.nextInt();
        double average = findAverage(num1, num2);

        switch (choice) {
            case 1:
                System.out.println(+num1 + " + " + num2 + " = ");
                break;
            case 2:
                System.out.println("The average of " + num1 + "and" + num2 + "is" + average);
                break;
            case 3:
                System.out.println("The amount in tax to be collected from a purchace of " + num1 + "and " + num2 + "is " + calcTax);
                break;
            case 4:
                System.out.println("You've chosen to quit.");
                System.exit(0);
                return;
            default:
                System.out.println("Invalid entry. Please try again.");
                selection = scanner.nextInt();
        }
        kb.close();
    }
}

您的程序仍然存在一些问题,即逻辑,您应该分析自己以发展自己的编程能力。

There are a few issues with your code. The first one I noticed is you didn't declare the "selection" variable where you try to assign a value to in the default case.

The error you are facing is due to not closing the main method, you simply missed an "}" after closing the while loop.

Here it is properly closed:

    public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    double num1, num2;
    boolean keepGoing = true;
    int choice;
    double answer = 0.0;
    double calcTax;

    System.out.print("Please give me a number: ");
    num1 = kb.nextDouble();
    System.out.print("Please give me another number: ");
    num2 = kb.nextDouble();

    while (keepGoing) {
        System.out.print("Which would you like to do? ");
        choice = kb.nextInt();
        double average = findAverage(num1, num2);

        switch (choice) {
            case 1:
                System.out.println(+num1 + " + " + num2 + " = ");
                break;
            case 2:
                System.out.println("The average of " + num1 + "and" + num2 + "is" + average);
                break;
            case 3:
                System.out.println("The amount in tax to be collected from a purchace of " + num1 + "and " + num2 + "is " + calcTax);
                break;
            case 4:
                System.out.println("You've chosen to quit.");
                System.exit(0);
                return;
            default:
                System.out.println("Invalid entry. Please try again.");
                selection = scanner.nextInt();
        }
        kb.close();
    }
}

There are still a few issues with your program, namely logic which you should analyze yourself to develop your programming ability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文