[英]I tried to write a basic calculator but java doesn't see my number, can't find what's wrong :(
// 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.");
}
This is my code, I want to get Welcome to "SimpleCalc"...这是我的代码,我想欢迎使用“SimpleCalc”...
+,-,*,/ value Clear +,-,*,/ 值 清除
Select: + 25.0选择:+ 25.0
+,-,*,/ value Clear +,-,*,/ 值 清除
Select: / 4选择:/4
+,-,*,/ value Clear +,-,*,/ 值 清除
Select: Clear选择:清除
+,-,*,/ value Clear +,-,*,/ 值 清除
Select: q选择:q
an output like this.这样的输出。 But something wrong and I can't find what's wrong.
但是出了点问题,我找不到问题所在。 And I get my output like this;
我得到这样的输出;
Welcome to "SimpleCalc"...欢迎使用“简单计算器”...
+,-,*,/ value Clear +,-,*,/ 值 清除
Select: + 25.0选择:+ 25.0
+,-,*,/ value Clear +,-,*,/ 值 清除
Select:选择:
Thanks for help.感谢帮助。
Here you go.干得好。
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.");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.