Java 我被要求创建一个循环,同时计算器在标准和科学两种模式下运行,并获取用户输入
要求用户输入计算器模式和操作。Java 我被要求创建一个循环,同时计算器在标准和科学两种模式下运行,并获取用户输入,java,loops,while-loop,calculator,Java,Loops,While Loop,Calculator,要求用户输入计算器模式和操作。 然后在获得用户输入后进行while循环。 我必须做所有的操作,一旦我收到输入,我似乎无法获得用户输入和计算代码 import java.util.Scanner; public class loopsProject { public static void main(String[] args) { System.out.println("Hello Codiva"); Scanner scnr = new Scanner(System.in
然后在获得用户输入后进行while循环。
我必须做所有的操作,一旦我收到输入,我似乎无法获得用户输入和计算代码
import java.util.Scanner;
public class loopsProject {
public static void main(String[] args) {
System.out.println("Hello Codiva");
Scanner scnr = new Scanner(System.in);
String mode;
String operator;
double numOne;
double numTwo;
double numThree;
double numofdValues;
String result;
//asking user to enter mode
System.out.print("Enter the calculator mode: Standard/Scientific?");
mode = scnr.nextLine();
while (mode.equals("Standard")) {
System.out.println("Enter '+' for addition, '-' for subtractions, '*'
for multiplication, '/' for division");
operator = scnr.nextLine();
}
while (numOne != 0 && numTwo != 0 && numThree !=0)
if (operator.equals("+")) {
System.out.println("How many numbers would you like to add?");
numofdValues = scnr.nextDouble();
System.out.println("Enter + numofdValues + numbers");
numOne = scnr.nextDouble;
numTwo = scnr.nextDouble;
numThree = scnr.nextDouble;
result = numOne + numTwo + numThree;
System.out.println("Your added answer is:" + result);
}
if (operator.equals("-")) {
System.out.println("How many numbers would you like to subtract?");
numofdValues = scnr.nextDouble();
System.out.println("Enter + numofdValues + numbers");
num1 = scnr.nextDouble;
num2 = scnr.nextDouble;
num3 = scnr.nextDouble;
result = numOne - numTwo - numThree;
System.out.println("Your subtracted answer is:" + result);
}
if (operator.equals("*"))
System.our.println("Your multiplied answer is:" + result);
if (operator.equals("/"))
if (operator.equals("invalid")) {
System.out.println("Your imput is invalid, please try again");
}
你提到你和我有麻烦 为整个问题设置用户输入 希望这将为您提供前进的结构。您有两个while循环,但您需要一个外部循环来控制用户选择模式或退出的循环
public class loopsProject {
public static void main(String[] args) {
System.out.println("Hello Codiva");
//Your variables here
//asking user to enter mode
System.out.print("Enter the calculator mode: Standard/Scientific?");
mode = scnr.nextLine();
//This loop controls the mode, user will keep on entering or they can quit
while (!mode.equals("Quit")) {
if (mode.equals("Standard")) {
//your Standard logic
}
else if (mode.equals("Scientific")) {
//your Scientific logic
}
else {
//Handle however you want or quit
break;
}
//Ask user for input again
System.out.print("Enter the calculator mode: Standard/Scientific?");
mode = scnr.nextLine();
}
}
}
如果操作无效,请重新提示用户
对于操作无效的情况,您可以做的只是告诉用户操作无效,然后continue
跳过向用户请求输入。这样,它将保留模式并在下一个循环中重新启动逻辑。每次需要验证用户的输入时,这将进入标准逻辑或科学逻辑
if (...) { //your checks for user input invalid operation here
System.out.println("Invalid operation, please retry.");
continue;
}
粘贴完整文件的内容并描述您的问题是什么?在本项目中,您将创建一个基于控制台的基本计算器程序。计算器可以在两种模式下运行:标准模式和科学模式。标准模式将允许用户执行以下+、-、*、/。科学模式可以做同样的事情,但是增加了3个操作sinx cosx和tanx。首先,它要求用户输入模式,然后要求用户输入操作…如果操作无效,请重新提示用户。然后它要求用户提供他们想要加、减、乘或除的双精度值。然后我必须显示结果并询问用户是否要重新开始我最大的问题是为整个问题设置用户输入。您能描述更多您的问题吗?