我正在嘗試開發一個計算器程式,該程式輸入表單的算術運算式number operator number =并計算運算式的結果。運算式將從左到右進行評估,不考慮常規運算子的優先級。例如,運算式14 - 5 * 3 =將產生27.0. 該值=顯示最終結果并終止程式。
這幾天我一直在嘗試解決這個問題,但是每當我輸入一個包含兩個以上數字的運算式時,它都會輸出錯誤的答案。例如,2.8 2 - 9.5應該相等-4.7但程式輸出-6.7. 知道為什么會這樣嗎?
import java.util.Scanner;
public class Calculator {
// Compute an arithmetic expression
public static void main(String[] args) {
// Declare the identifiers
final String END = "=";
String input;
double num1 = 0;
double num2 = 0;
char operator = 0;
Scanner scnr = new Scanner (System.in);
System.out.println("Enter your numeric expression in the following form: ");
System.out.println("number operator number operator number = ");
System.out.println("Leave a blank space after each number or operator.");
System.out.println("Example: 3.5 * 3 - 5 / 2.5 =");
// Input the first item
System.out.print("> ");
input = scnr.next();
// Process the first item and input and process the rest of the items
while (!input.equals(END)){
switch (input){
case " ":
operator = ' ';
System.out.println("> Operator is: " operator);
break;
case "-":
operator = '-';
System.out.println("> Operator is: " operator);
break;
case "*":
operator = '*';
System.out.println("> Operator is: " operator);
break;
case "/":
operator = '/';
System.out.println("> Operator is: " operator);
break;
default: // a number was entered
if (num1 == 0) {
num1 = Double.parseDouble(input);
System.out.println("> Num1 is: " num1);
}
else {
num2 = Double.parseDouble(input);
System.out.println("> Num2 is: " num2);
}
} // end of switch
if (num1 != 0 && num2 != 0) {
System.out.println("Num2 before calc is " num2);
switch (operator) {
case ' ':
num2 = num1 num2;
break;
case '-':
num2 = num1 - num2;
break;
case '*':
num2 = num1 * num2;
break;
case '/':
num2 = num1 / num2;
break;
default:
}
}
input = scnr.next();
} // end of while-loop
// Display the answer
System.out.println("> Answer is: " num2);
System.out.println("Have a nice day!");
}
}
uj5u.com熱心網友回復:
為了使其作業,請嘗試:
- 在您的第二個 switch 陳述句中,更改
num2 = num1 num2;為num1 = num1 num2;. 對所有情況都這樣做; - 如果輸入是運算子,我添加了一個
isOperator布林值以跳過計算操作。
完整代碼如下:
import java.util.Scanner;
public class Calculator {
// Compute an arithmetic expression
public static void main(String[] args) {
// Declare the identifiers
final String END = "=";
String input;
double num1 = 0;
double num2 = 0;
char operator = 0;
boolean isOperator;
Scanner scnr = new Scanner (System.in);
System.out.println("Enter your numeric expression in the following form: ");
System.out.println("number operator number operator number = ");
System.out.println("Leave a blank space after each number or operator.");
System.out.println("Example: 3.5 * 3 - 5 / 2.5 =");
// Input the first item
System.out.print("> ");
input = scnr.next();
// Process the first item and input and process the rest of the items
while (!input.equals(END)){
isOperator = true;
switch (input){
case " ":
operator = ' ';
System.out.println("> Operator is: " operator);
break;
case "-":
operator = '-';
System.out.println("> Operator is: " operator);
break;
case "*":
operator = '*';
System.out.println("> Operator is: " operator);
break;
case "/":
operator = '/';
System.out.println("> Operator is: " operator);
break;
default: // a number was entered
isOperator = false;
if (num1 == 0) {
num1 = Double.parseDouble(input);
System.out.println("> Num1 is: " num1);
}
else {
num2 = Double.parseDouble(input);
System.out.println("> Num2 is: " num2);
}
} // end of switch
// do not compute the operation if the input is an operator and num1,num2 != 0
if (num1 != 0 && num2 != 0 && !isOperator) {
System.out.println("Num2 before calc is " num2);
switch (operator) {
case ' ':
num1 = num1 num2;
break;
case '-':
num1 = num1 - num2;
break;
case '*':
num1 = num1 * num2;
break;
case '/':
num1 = num1 / num2;
break;
default:
}
}
input = scnr.next();
} // end of while-loop
// Display the answer
System.out.println("> Answer is: " num1);
System.out.println("Have a nice day!");
}
}
編輯:如評論中所述,代碼不處理用戶輸入時的情況0。下面,我洗掉了if(num1 == 0)和if (num1 != 0 && num2 != 0)條件:
import java.util.Scanner;
public class Calculator {
// Compute an arithmetic expression
public static void main(String[] args) {
// Declare the identifiers
final String END = "=";
String input;
double result = 0;
double num = 0;
char operator = 0;
boolean isOperator;
Scanner scnr = new Scanner (System.in);
System.out.println("Enter your numeric expression in the following form: ");
System.out.println("number operator number operator number = ");
System.out.println("Leave a blank space after each number or operator.");
System.out.println("Example: 3.5 * 3 - 5 / 2.5 =");
// Input the first item
System.out.print("> ");
input = scnr.next();
// Process the first item and input and process the rest of the items
while (!input.equals(END)){
isOperator = true;
switch (input){
case " ":
operator = ' ';
System.out.println("> Operator is: " operator);
break;
case "-":
operator = '-';
System.out.println("> Operator is: " operator);
break;
case "*":
operator = '*';
System.out.println("> Operator is: " operator);
break;
case "/":
operator = '/';
System.out.println("> Operator is: " operator);
break;
default: // a number was entered
isOperator = false;
num = Double.parseDouble(input);
System.out.println("> Num is: " num);
} // end of switch
// do not compute the operation if the input is an operator
if (!isOperator) {
System.out.println("Result before calc is " result);
switch (operator) {
case ' ':
result = num;
break;
case '-':
result -= num;
break;
case '*':
result *= num;
break;
case '/':
result /= num;
break;
default:
result = num;
}
}
input = scnr.next();
} // end of while-loop
// Display the answer
System.out.println("> Answer is: " result);
System.out.println("Have a nice day!");
}
}
uj5u.com熱心網友回復:
我稍微調高了您的訂單并重置了持有變數。
public static void main(String[] args) {
// Declare the identifiers
final String END = "=";
String input;
double num1 = 0;
double num2 = 0;
char operator = 0;
Scanner scnr = new Scanner (System.in);
System.out.println("Enter your numeric expression in the following form: ");
System.out.println("number operator number operator number = ");
System.out.println("Leave a blank space after each number or operator.");
System.out.println("Example: 3.5 * 3 - 5 / 2.5 =");
// Input the first item
System.out.print("> ");
input = scnr.next();
// Process the first item and input and process the rest of the items
while (!input.equals(END)){
switch (input){
case " ":
operator = ' ';
System.out.println("> Operator is: " operator);
break;
case "-":
operator = '-';
System.out.println("> Operator is: " operator);
break;
case "*":
operator = '*';
System.out.println("> Operator is: " operator);
break;
case "/":
operator = '/';
System.out.println("> Operator is: " operator);
break;
default: // a number was entered
if (num1 == 0) {
num1 = Double.parseDouble(input);
System.out.println("> Num1 is: " num1);
} else {
num2 = Double.parseDouble(input);
System.out.println("> Num2 is: " num2);
}
} // end of switch
if (num1 != 0 && num2 != 0) {
System.out.println(String.format("Num1 : %.3f, Num2: %.3f", num1, num2));
switch (operator) {
case ' ':
num1 = num1 num2;
num2 = 0;
break;
case '-':
num1 = num1 - num2;
num2 = 0;
break;
case '*':
num1 = num1 * num2;
num2 = 0;
break;
case '/':
num1 = num1 / num2;
num2 = 0;
break;
default:
}
}
input = scnr.next();
} // end of while-loop
// Display the answer
System.out.println("> Answer is: " num1);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/449136.html
