![](/img/trans.png)
[英]Java Udemy course leap year calculator - can't figure out what am I doing wrong
[英]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.");
}
這是我的代碼,我想歡迎使用“SimpleCalc”...
+,-,*,/ 值 清除
選擇:+ 25.0
+,-,*,/ 值 清除
選擇:/4
+,-,*,/ 值 清除
選擇:清除
+,-,*,/ 值 清除
選擇:q
這樣的輸出。 但是出了點問題,我找不到問題所在。 我得到這樣的輸出;
歡迎使用“簡單計算器”...
+,-,*,/ 值 清除
選擇:+ 25.0
+,-,*,/ 值 清除
選擇:
感謝幫助。
干得好。
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.