我有这个计算器代码,使用用户后缀表达式和文件输入,具体取决于用户的偏好,我需要创建一个循环,如果任何后缀表达式不正确,(3 3 +,3 x它已经为用户提供了有关错误的正确错误消息,但我需要它再次为文件输入和键盘输入启动循环,从用户输入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");
}
}
}
}
答案 0 :(得分:0)
看起来您可以通过非常强大的方式利用这些功能来解决您的问题。我还注意到你可能想仔细看看你如何使用和命名你的变量。
函数或方法是任何编程语言中非常重要的一部分,当你可以执行某些事情并发现自己想要再次某事时,这就是你可以做到的确定你想要一个函数或方法的位置。
您已完成上述难题,因为您的代码可以完成您希望它执行的操作。现在你想再次这样做,所以我们可以把这个动作想象成一个函数。
如果用户按下' X'您可能希望添加退出该功能的功能。或类似的东西,如果你选择实施它,我会把这个练习留给你。
有些情况下,如果您已经有权访问变量,则初始化变量,对我来说最明显的是Scanner。当您声明并初始化Scanner Scanner = new Scanner(System.in)
时,只要您有权访问Scanner变量,就可以访问System.in(另一个提示是使用小写字母命名变量,其中Scanner和Scanner稍后不明确关于你是否引用变量或类)。后来做Scanner Scanner2 = new Scanner(System.in)
是不必要的。
您可以对变量进行一些其他清理工作,但我再次将它们留给您。
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);
boolean quitting = false;
while (!quitting){
quitting = processInput(scanner);
}
}
public static boolean processInput(Scanner scanner){
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")) {
System.out.println("Please enter a postfix expression: ");
String line = scanner.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 {
System.out.print("Enter the file name to read expressions from : ");
File file = new File(scanner.nextLine());
Scanner fileScanner = new Scanner(file);
while (!(file.exists())) {
System.out.println("Error: That file does not exist, please re-enter: ");
}
String line;
while (fileScanner.hasNext()) {
line = fileScanner.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");
}
}
return false;
}
}
答案 1 :(得分:0)
尝试使用while(true)循环并在输入可接受时中断。
一个简单的例子:
int input=0;
while(true){
input =new Scanner(System.in).nextInt();
if(input<=0){
System.out.println("Illegal input!");
continue;
}
break;
}
答案 2 :(得分:0)
NB:
- 您应该在JAVA中使用小写字母作为变量名称,例如Scanner scanner
。
- 您应为每个来源使用一个扫描程序,例如System.in
或new File("name of file")
。
- 对于键盘扫描仪(System.in),建议使用static
扫描仪变量,如:private static Scanner scanner = new Scanner(System.in);
。
- 当您使用具有路径文件Scanner(File source)
如果您想从头开始重新循环,可以尝试使用这样的无限循环while(true)
:
public class Test {
// lower case of name variable
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException {
while(true){
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")) {
System.out.println("Please enter a postfix expression: ");
String line = scanner.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 {
System.out.print("Enter the file name to read expressions from : ");
File file = new File(scanner.nextLine());
Scanner scanner1 = new Scanner(file);
while (!(file.exists())) {
System.out.println("Error: That file does not exist, please re-enter: ");
}
String line;
while (scanner1.hasNext()) {
line = scanner1.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");
}
}
}
// close your Scanner
scanner1.close();
} catch (FileNotFoundException exception) {
System.out.println("The file could not be found");
}
}
}
}
}