【问题标题】:How do I correctly format my code to run this menu program in Java? [closed]如何正确格式化我的代码以在 Java 中运行此菜单程序? [关闭]
【发布时间】:2022-06-10 17:22:51
【问题描述】:

这是一个初学者的问题。我即将开始一个在线训练营,并且被困在我的最后一个工作前项目(这是我们被指示自学的所有内容的累积)。我真的迷路了,无法解决这个问题。

这是我目前的想法:

import java.util.Scanner;

public class Objective9Lab5 {

  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();
    }
    public static void printMenu() {
        System.out.println();
        System.out.println("========= MENU =========");
        System.out.println("|                      |");
        System.out.println("|   1. Add Numbers     |");
        System.out.println("|   2. Find Average    |");
        System.out.println("|   3. Calculate Tax   |");
        System.out.println("|   4. Exit            |");
        System.out.println("|                      |");
        System.out.println("========================");
        System.out.println();
    }

    public static double findSum(double x, double y) {
        double sum = x + y;
        return sum;
  }
  public static double findAverage(double x, double y){
    double average = (x + y) / 2;
    return average;
  }
  public static double findCalcTax(double x, double y){
    double sum * .831
    }

我的输出应该是这样的:

Please give me a number: 2
Please give me another number: 8

========= MENU =========
|                      |
|   1. Add Numbers     |
|   2. Find Average    |
|   3. Calculate Tax   |
|   4. Exit            |
|                      |
========================

Which would you like to do? 1
2.0 + 8.0 = 10.0

========= MENU =========
|                      |
|   1. Add Numbers     |
|   2. Find Average    |
|   3. Calculate Tax   |
|   4. Exit            |
|                      |
========================

Which would you like to do? 2
The average of 2.0 and 8.0 is: 5.0

========= MENU =========
|                      |
|   1. Add Numbers     |
|   2. Find Average    |
|   3. Calculate Tax   |
|   4. Exit            |
|                      |
========================

Which would you like to do? 3
The amount in tax to be collected from a purchase of 2.0 and 8.0 is: 0.831

========= MENU =========
|                      |
|   1. Add Numbers     |
|   2. Find Average    |
|   3. Calculate Tax   |
|   4. Exit            |
|                      |
========================

Which would you like to do? 5
Invalid entry.  Please try again

========= MENU =========
|                      |
|   1. Add Numbers     |
|   2. Find Average    |
|   3. Calculate Tax   |
|   4. Exit            |
|                      |
========================

Which would you like to do? 4
You've chosen to quit.

我不断收到此错误,但对于所有主要方法:

Objective9Lab5.java:59: error: illegal start of expression
    public static double findSum(double x, double y) {
    ^

【问题讨论】:

  • main() 后面的大括号不闭合;在问题中标记的行之前添加},它应该可以编译。之后,您可以尝试相应地重新格式化……

标签: java methods main


【解决方案1】:

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

您面临的错误是由于没有关闭 main 方法,您只是在关闭 while 循环后错过了一个“}”。

这里是正确关闭的:

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

你的程序还有一些问题,就是逻辑需要分析自己来培养你的编程能力。

【讨论】:

  • 其他一些问题:scanner 未定义。 System.exit(0); 是不必要的;从main 返回就足够了。 kb.close() 不合适; Scanner 是从 System.in 读取创建的,但此关闭并未打开它,因此此代码不应关闭它。关闭你打开的一切;不要不要关闭你没有打开的任何东西。
猜你喜欢
  • 2021-08-10
  • 1970-01-01
  • 2010-09-07
  • 2021-11-07
  • 2012-01-14
  • 2017-02-16
  • 1970-01-01
  • 2019-07-21
相关资源
最近更新 更多