【问题标题】:How can I write a program that asks multiple choice questions then performs input actions for each answer?如何编写一个程序来询问多项选择题,然后为每个答案执行输入操作?
【发布时间】:2020-04-24 13:43:08
【问题描述】:

我正在尝试编写一个程序,在该程序中,用户必须从多项选择题中选择一个答案。如果该用户从三个选项中选择一个,则用户必须输入一个数字供程序计算。我想让程序问的问题是“你想计算平方根、对数还是阶乘?”。另外,我能否将 if-else 条件合并到同一个程序中?到目前为止,这是我的代码:

System.out.println("Would you like to calculate?");
String choice = new choice[3];
choice[0] = "Square root";
choice[1] = "Logarithm";
choice[2] = "Factorial";
choice = scan.nextString();

if (choice = "Square root") {
    System.out.println("Enter the number to square root: ");
    double x = scan.nextDouble();
    System.out.println(Math.sqrt(x));
} else if (choice = "Logarithm") {
    System.out.println("Enter the number to log: ");
    double x = scan.nextDouble();
    System.out.println(Math.log(x));
} else {
    System.out.println("Enter the number to do factorial: ");
    double x = scan.nextDouble();
    System.out.println(factorial(x));
}

我觉得我这里的代码不正确。这是我第一次编写提出多项选择题的程序。如果您对如何纠正此问题有任何建议,请告诉我。

谢谢。

【问题讨论】:

  • 您不清楚为什么您的代码不正确。您可能希望将重点放在您无法解决的特定问题上。
  • choice = "Square root" 不是您在 Java 中比较字符串的方式

标签: java function math input choice


【解决方案1】:

你可以这样做

 System.out.println("Would you like to calculate?");
    System.out.println("1- Square root/n2- Logarithm\n3- Factorial");

    Scanner scan = new Scanner(System.in);
    int choice = scan.nextInt();

    if (choice == 1) {
        System.out.println("Enter the number to square root: ");
        double x = scan.nextDouble();
        System.out.println(Math.sqrt(x));
    } else if (choice == 2) {
        System.out.println("Enter the number to log: ");
        double x = scan.nextDouble();
        System.out.println(Math.log(x));
    } else if (choice == 3) {
        System.out.println("Enter the number to do factorial: ");
        double x = scan.nextDouble();
        System.out.println(factorial(x));
    } else {
        System.out.println("Invalid choice");
    }

您还比较了 if 和 = 符号中的字符串。这是错误的。我们使用“.equal()”方法来比较java中的字符串。例如

if (choice.equal("Factorial");

如果您想像您的方法一样执行此操作。然后你有一些语法错误。第 2 行应该是 new String() 而不是 newchoice()。另外,你为什么要制作字符串数组?正确的代码应该是这样的。

System.out.println("Would you like to calculate?");

String choice = scan.nextString();

if (choice.equal("Square root")) {
    System.out.println("Enter the number to square root: ");
    double x = scan.nextDouble();
    System.out.println(Math.sqrt(x));
} else if (choice.equal("Logarithm")) {
    System.out.println("Enter the number to log: ");
    double x = scan.nextDouble();
    System.out.println(Math.log(x));
} else if (choice.equal("Factorial") {
    System.out.println("Enter the number to do factorial: ");
    double x = scan.nextDouble();
    System.out.println(factorial(x));
}

【讨论】:

  • 感谢您的建议。但是,由于我现在在 if-else 条件下使用“.equal()”,我应该用什么替换“int choice = scan.nextInt();”现在呢?
  • @GGomes4 看到我更新的答案。另外,如果对你有帮助,请点赞。
猜你喜欢
  • 2023-01-02
  • 2023-03-04
  • 2017-09-24
  • 1970-01-01
  • 2011-07-24
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 2021-10-13
相关资源
最近更新 更多