[英]How can I get my loop to keep re-entering new variables and operators to continue to calculate?
我试图让用户输入多个值,然后计算机将返回答案。 例如,他们将进入10 + 11 - 15/12? 然后它会输出答案。 我测试了这个,它只需要输入第一个数字和最后一个运算符以及最后一个数字并计算出答案。 (输出大概是10/12)从我可以告诉我的数学方法是正常的,但我也附加了它们。 如何让它继续读入新的用户输入然后进行数学计算以找出答案?
这是我的代码:
static void evaluate (){
// this loads a scanner and prompts the user for input
Scanner input = new Scanner(System.in);
System.out.println("Enter the expression: ");
int answer = 0;
int num = 0;
int input1, input2;
char operator = '+' ;
input1 = input.nextInt();
operator = input.next().charAt(0);
input2 = input.nextInt();
if(operator == '?')
return;
//this loop will continue until ? is entered
do{
if(operator == '?')
break;
operator = input.next().charAt(0);
input2 = input.nextInt();
// this will detect which operator is used
switch (operator) {
case '+': answer = addition(input1, input2);
break;
case '-': answer = subtract(input1, input2);
break;
case '*': answer = multiply(input1, input2);
break;
case '/': answer = division(input1, input2);
break;
case '%': answer = remainder(input1, input2);
} } while (operator != '?');
System.out.println("The result is " + answer);
}
这些是加法,减法,除法,乘法和模数的方法。
// this is the math if a + is used as an operator
static int addition (int num1, int num2){
int add;
add = num1 + num2;
return add;
}
// this is the math if a - is used as an operator
static int subtract (int num1, int num2){
int minus;
minus = num1 - num2;
return minus;
}
// this is the math if a * is used as an operator
static int multiply (int num1, int num2){
int multi;
multi = num1 * num2;
return multi;
}
// this is the math if a / is used as an operator
static int division (int num1, int num2){
int div;
div = num1 / num2;
return div;
}
// this is the math if a % is used as an operator
static int remainder (int num1, int num2){
int modulos;
modulos = num1 % num2;
return modulos;
}
你的逻辑非常好, if(operator == '?') break;
你所要做的就是放置这个条件if(operator == '?') break;
通过执行operator = input.next().charAt(0);
读取operator
的值之后operator = input.next().charAt(0);
。
另一件事是,您永远不会更改input1
的值。 执行任何操作后,您应该开始将answer
分配回input1
。
这是修改后的代码段:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter The Expression: ");
int answer = 0, num = 0, input1, input2;
char operator = '+' ;
/* Read Input 1 */
input1 = input.nextInt();
//this loop will continue until ? is entered
do {
/* Read Operator */
operator = input.next().charAt(0);
if(operator == '?') break;
/* Read Input 2 */
input2 = input.nextInt();
// this will detect which operator is used
switch (operator) {
case '+': answer = addition(input1, input2);
break;
case '-': answer = subtract(input1, input2);
break;
case '*': answer = multiply(input1, input2);
break;
case '/': answer = division(input1, input2);
break;
case '%': answer = remainder(input1, input2);
}
/* Input1 will be the answer now */
input1 = answer;
} while (operator != '?');
System.out.println("Result: " + answer);
}
// this is the math if a + is used as an operator
static int addition (int num1, int num2){
int add;
add = num1 + num2;
return add;
}
// this is the math if a - is used as an operator
static int subtract (int num1, int num2){
int minus;
minus = num1 - num2;
return minus;
}
// this is the math if a * is used as an operator
static int multiply (int num1, int num2){
int multi;
multi = num1 * num2;
return multi;
}
// this is the math if a / is used as an operator
static int division (int num1, int num2){
int div;
div = num1 / num2;
return div;
}
// this is the math if a % is used as an operator
static int remainder (int num1, int num2){
int modulos;
modulos = num1 % num2;
return modulos;
}
输入:
10 + 11 - 15 / 2 ?
输出:
Result: 3
此外,我希望您在输入数字之间加上空格,就像您在上面的问题中所显示的那样。 请注意,您正在阅读输入10 + 11 - 15 / 12 ?
会工作正常,但10+11-15/12?
将不会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.