【发布时间】:2016-09-28 19:08:53
【问题描述】:
我正在尝试为课堂制作一个基本的计算器。
我想出了下面的代码,但它所做的只是添加。
即使我使用-、* 或/。
有什么建议吗?
Scanner input = new Scanner(System.in);
String num1, num2, operation, function;
System.out.println("Enter a simple equation: ");
function = input.nextLine();
int firstspace = function.indexOf(" ");
num1 = (function.substring(0,firstspace));
int lastspace = function.lastIndexOf(" ");
operation = (function.substring(firstspace,lastspace));
num2= (function.substring(lastspace+1,function.length()));
double n1 = Double.parseDouble(num1);
double n2 = Double.parseDouble(num2);
if (operation.equals(" + "));
{
System.out.println("your answer is " + (n1 + n2));
}
if (operation.equals(" - "))
{
System.out.println("your answer is " + (n1 - n2));
}
if (operation.equals(" / "))
{
System.out.println("your answer is " + (n1 / n2));
}
if (operation.equals(" * "))
{
System.out.println("your answer is " + ( n1*n2));
}
}
}
【问题讨论】:
-
欢迎来到 SO :-) 你试过调试你的代码吗?
标签: java if-statement calculator