【发布时间】: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