我有這個計算器代碼,它使用用戶後綴表達式和一個文件輸入取決於用戶的偏好,我需要創建一個循環,如果任何後綴表達式不正確,(3 3 +,3 x 3,34 43 43等)它已經給用戶一個關於錯誤的正確的錯誤信息,但我需要它再次啓動文件輸入和鍵盤輸入循環,從開頭用戶在k或f中輸入他們想要的輸入。循環計算器代碼
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner Scanner = new Scanner(System.in);
System.out.println("Press K to enter the input using the keyboard. Press F to read expressions from a file");
String option = Scanner.nextLine();
while (!(option.equalsIgnoreCase("K") || option.equalsIgnoreCase("F"))) {
System.out.println("Invalid option - please choose again");
option = Scanner.nextLine();
}
if (option.equalsIgnoreCase("K")) {
Scanner Scanner2 = new Scanner(System.in);
System.out.println("Please enter a postfix expression: ");
String line = Scanner2.nextLine();
String[] elements = line.split(" ");
if (elements.length != 3) {
System.out.println("Error: Invalid Expression: " + line);
}
else {
try {
// "try" is a special statement which allows us to deal with
// "exceptions"
double num1 = Double.parseDouble(elements[0]);
double num2 = Double.parseDouble(elements[1]);
double answer = 0;
boolean OpperationWrong = false;
String operation = elements[2];
switch (operation) {
case "+": {
answer = (num1 + num2);
System.out.println("Answer is: = " + answer);
}
break;
case "-": {
answer = (num1 - num2);
System.out.println("Answer is: = " + answer);
}
break;
case "/": {
answer = (num1/num2);
System.out.println("Answer is: = " + answer);
}
break;
case "*": {
answer = (num1 * num2);
System.out.println("Answer is: = " + answer);
}
break;
default:
OpperationWrong = true;
}
String ans;
if (OpperationWrong) {
System.out.println("Please use +,*,/,- for your operator");
} else {
ans = String.valueOf(answer);
}
} catch (NumberFormatException e) {
System.out.println("Invalid Key entered, please enter a number for first 2 elements");
}
}
} else if (option.equalsIgnoreCase("F")) {
try {
Scanner Scanner3 = new Scanner(System.in);
System.out.print("Enter the file name to read expressions from : ");
File file = new File(Scanner3.nextLine());
Scanner3 = new Scanner(file);
while (!(file.exists())) {
System.out.println("Error: That file does not exist, please re-enter: ");
}
String line;
while (Scanner3.hasNext()) {
line = Scanner3.nextLine(); // read the next line of text
// from the file
String[] elements = line.split(" ");
if (elements.length != 3) {
System.out.println("Error: Invalid Expression: " + line);
} else {
try {
// "try" is a statement which allows us to deal with
// "exceptions"
double num1 = Double.parseDouble(elements[0]);
double num2 = Double.parseDouble(elements[1]);
double answer = 0;
boolean OpperationWrong = false;
String operation = elements[2];
switch (operation) {
case "+": {
answer = (num1 + num2);
System.out.println("Answer is: = " + answer);
}
break;
case "-": {
answer = (num1 - num2);
System.out.println("Answer is: = " + answer);
}
break;
case "/": {
answer = (num1/num2);
System.out.println("Answer is: = " + answer);
}
break;
case "*": {
answer = (num1 * num2);
System.out.println("Answer is: = " + answer);
}
break;
default:
OpperationWrong = true;
}
String ans;
if (OpperationWrong) {
ans = " Please use +,*,/,- for your operator";
} else {
ans = String.valueOf(answer);
}
} catch (NumberFormatException e) {
System.out.println("Invalid expression please enter a number for first 2 elements");
}
}
}
} catch (FileNotFoundException exception) {
System.out.println("The file could not be found");
}
}
}
}
檢查這題。 http://stackoverflow.com/questions/33585160/create-a-java-calculator/33585372#33585372 – panagdu