制作一个简单的计算器:无法退出循环或给出答案 [英] Making a simple calculator: cannot exit loop or give answer
问题描述
我需要制作一个简单的计算器。我输入任意数量的运算符和操作数时遇到问题,然后在输入等号键时输出答案。
I need to make a simple calculator. I am having trouble with entering any amount of operators and operands, then outputting the answer when the equals button has been entered.
到目前为止,如果我只按一个数字并且它退出的数字,但没有给我答案。如果我执行多个运算符和操作数,那么 =
它不会退出循环。
例如它应该是:
So far, if I just press one number and a digit it exits but does not give me an answer. If I do more than one operator and operand then =
it does not exit the loop.
For example it should be like:
5
+
5
+
5
=
15
这是我的代码,计算器:
Here is my code, Calculator:
public interface Calculator {
public void setOperator(char operator); // eg +-*/=
public void setOperand (double operand); // eg 123.456
public double getResult();
}
SimpleCalculator:
SimpleCalculator:
import java.io.*;
public class SimpleCalculator implements Calculator {
char operator;
double operand;
double result;
double answer;
public void setOperator(char operator){
this.operator = operator;
}
public char getOperator(){
return operator;
}
public void setOperand(double operand){
this.operand = operand;
}
public double getOperand(){
return operand;
}
public double getResult(){
if (getOperator() == '+'){
result = (getOperand() + getOperand());
}
if (getOperator() == '-'){
result = (getOperand() - getOperand());
}
if (getOperator() == '*'){
result = (getOperand() * getOperand());
}
if (getOperator() == '/')
{
result = (getOperand() / getOperand());
}
if (getOperator() == '=')
result = answer;
}
return result;
}
public boolean getanswer(String value)
{
boolean isnum = false;
try {
setOperand(Double.parseDouble(value));
operand = (Double.parseDouble(value));
getResult();
isnum = true;
}
catch(Exception e)
{
try {
setOperator(value.charAt(0));
operator = (value.charAt(0));
isnum = false;
}
catch(Exception e2)
{
{
System.out.println("Enter a number");
}
}
return isnum;
}
}
SimpleTest:
SimpleTest:
import java.io.*;
public class SimpleTest{
static String value;
static double operand;
static char operator;
static boolean isnum;
public static void main(String[] argv){
SimpleCalculator calculator = new SimpleCalculator();
value = UserInput.readString();
while (!(value.equals("=")))
{
isnum = calculator.getanswer(value);
if (!(isnum == true))
{
break;
}
}
System.out.println(calculator.getResult());
}
}
推荐答案
根据您问题的标题,我发现您的主循环可能会出现问题:
Based on the title of your question I found that you might see an issue with your main-loop:
value = UserInput.readString();
while (!(value.equals("="))) {
isnum = calculator.getanswer(value);
if (!(isnum == true)) {
break;
}
}
由于您在循环外读取用户输入,因此永远不会改变,这将只运行一次(如果isnum为假)或无限运行(如果isnum为真) - getanswer没有关于其结果的内存。因此,如果你输入一个数字,它将永远循环,但没有做任何有用的事情。
Since you read the user input outside the loop it will never change and this will either run only once (if isnum is false) or infinitely (if isnum is true) -- getanswer does not has a memory with respect to its result. Thus if you input a number it will loop forever but not doing anything useful.
请注意:这只是第一个猜测。我没有检查你的其他程序。
Please note: this is just a first guess. I didn't check the rest of your program.
这篇关于制作一个简单的计算器:无法退出循环或给出答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!