// constants
final String LINE = "----------------";
final String VALUE = " +,-,*,/ value";
final String CLEAR = "Clear";
final String QUIT = "Quit ";
final int ZERO = 0;
// variables
double first;
String function;
double number;
// program code
System.out.println( "Start...");
System.out.println( "Welcome to \"SimpleCalc\" ... ");
first = 0;
// 1.Calculations
do
{
System.out.println(LINE);
System.out.println( "[" + first + "]" );
System.out.println(VALUE);
System.out.println(CLEAR);
System.out.println(QUIT);
System.out.println(LINE);
System.out.println(" SELECT :");
function = scan.next();
if (function.equals("+") || function.equals("-") || function.equals("*") || function.equals("/"))
{
number = scan.nextDouble();
if ( function.equals("+") )
{
first = first + number;
}
else if (function.equals("-") )
{
first = first - number;
}
else if (function.equals("/") )
{
first = first / number;
}
else if (function.equals("*") )
{
first = first * number;
}
}
else if (function.equals("Clear") );
{
first = ZERO;
}
}
while ( function != "q" );
//2. Exit
// todo...
System.out.println( "End.");
}
这是我的代码,我想得到 欢迎来到" SimpleCalc" ...
+, - ,*,/ value 清除
选择:+ 25.0
+, - ,*,/ value 清除
选择:/ 4
+, - ,*,/ value 清除
选择:清除
+, - ,*,/ value 清除
选择:q
像这样的输出。但是有些不对劲我无法找到错误。我得到了这样的输出;欢迎来到" SimpleCalc" ...
+, - ,*,/ value 清除
选择:+ 25.0
+, - ,*,/ value 清除
选择
感谢您的帮助。
答案 0 :(得分:1)
你走了。
import java.util.Scanner;
public class Calculator {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
// constants
final String LINE = "----------------";
final String VALUE = " +,-,*,/ value";
final String CLEAR = "Clear";
final String QUIT = "Quit";
final int ZERO = 0;
// variables
double result;
String function;
double number;
// program code
System.out.println("Start...");
System.out.println("Welcome to \"SimpleCalc\" ... ");
result = 0;
// 1.Calculations
while (true) {
System.out.println(LINE);
System.out.println("[" + result + "]");
System.out.println(VALUE);
System.out.println(CLEAR);
System.out.println(QUIT);
System.out.println(LINE);
System.out.println(" SELECT :");
function = scan.next();
if (function.equalsIgnoreCase("q")) {
break;
}
if (function.equalsIgnoreCase("Clear")) {
result = ZERO;
} else {
number = scan.nextDouble();
switch (function) {
case "+":
result = result + number;
break;
case "-":
result = result - number;
break;
case "/":
result = result / number;
break;
case "*":
result = result * number;
break;
}
}
}
//2. Exit
// todo...
System.out.println("End.");
}
}