在程序终止时难以存储要打印的以下统计信息。我已经能够存储有效和无效表达式的总数,但是我对以下内容有困难:
最高整体结果值。
总体结果最低值。
所有结果值的汇总,即所有结果加在一起。
平均结果值
非常感谢任何上述任何指导。
Scanner input = new Scanner(System.in); // Creating new scanner to take user choice input
int validExpressions = 0;
int invalidExpressions = 0;
int lowestval = 0;
int highestval;
while (true) {
System.out.println("Enter K to enter a postfix expression or F to open a text file:"); // requesting user input
String choice = input.nextLine(); // Taking user input from keyboard, either K for manual entry or F for file input
float answer; // Variable used to store answers.
if ("F".equals(choice)) {
System.out.println("Please enter the name of a .txt file:"); // user prompted to enter the name of the text file
String file = input.nextLine(); // File name will be stored in a String (file) to be used later
File txtfile = new File(file);
Scanner reader = new Scanner(txtfile); // Creating a new scanner which reads the text file
while (reader.hasNextLine()) { // The new scanner (reader) reads each line of the text file
String expression = reader.nextLine(); // Each line is stored in a String called Expression
String[] parts = expression.split(" "); // The string is split into three parts; two numbers and an operator
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];
float number1 = Float.parseFloat(part1); // Parts one and two are both converted from a String to a Float
float number2 = Float.parseFloat(part2);
// Program processes the operator and applies it to Float number1 and Float number2 before outputting the
// expression and the correct answer
if ("+".equals(part3)) {
answer = number1 + number2;
System.out.println(part1 + " " + "+" + " " + part2 + " " + "=" + " " + answer);
validExpressions++;
} else if ("-".equals(part3)) {
answer = number1 - number2;
System.out.println(part1 + " " + "-" + " " + part2 + " " + "=" + " " + answer);
validExpressions++;
} else if ("*".equals(part3)) {
answer = number1 * number2;
System.out.println(part1 + " " + "*" + " " + part2 + " " + "=" + " " + answer);
validExpressions++;
} else if ("/".equals(part3)) {
answer = number1 / number2;
System.out.println(part1 + " " + "/" + " " + part2 + " " + "=" + " " + answer);
validExpressions++;
}
}
} else if ("K".equals(choice)) // If the user enters K input will be taken from the keyboard
System.out.println("Please enter a post-fix expression:"); // User is prompted to enter a post fix expression
Scanner expression = new Scanner(System.in); // Creating a new Scanner called Expression to read user input
String exp = expression.nextLine(); // The user's input is stored in a string
String[] elements = exp.split(" "); // Expression is split into three elements, two numbers and an operator
String element1 = elements[0];
String element2 = elements[1];
String element3 = elements[2];
float num1 = Float.parseFloat(element1); // Element1 and element2 are converted to Floats and named num1 and num2
float num2 = Float.parseFloat(element2);
// Program processes the operator and applies it to Float num1 and Float num2 before outputting the
// expression and the correct answer
if ("+".equals(element3)) {
answer = num1 + num2;
System.out.println(element1 + " " + "+" + " " + element2 + " " + "=" + " " + answer);
validExpressions++;
} else if ("-".equals(element3)) {
answer = num1 - num2;
System.out.println(element1 + " " + "-" + " " + element2 + " " + "=" + " " + answer);
validExpressions++;
} else if ("*".equals(element3)) {
answer = num1 * num2;
System.out.println(element1 + " " + "*" + " " + element2 + " " + "=" + " " + answer);
validExpressions++;
} else if ("/".equals(element3)) {
answer = num1 / num2;
System.out.println(element1 + " " + "/" + " " + element2 + " " + "=" + " " + answer);
validExpressions++;
if (choice.isEmpty()) { // If the user does not enter K or F the program will exit
System.out.println("Evaluations Complete");
System.out.println("-------------------------");
System.out.println("Valid expressions: " + validExpressions);
System.out.println("Invalid expressions: " + invalidExpressions);
System.exit(0);
}
}
}
}
}
答案 0 :(得分:0)
我在逻辑和语法上发生了一些错误的改变,但我没有对它进行过调整,你可能会尝试一下。
Scanner input = new Scanner(System.in); // Creating new scanner to take user choice input
int validExpressions = 0;
int invalidExpressions = 0;
float lowestval = 0;
float highestval =0 ;
float total=0;
while (true) {
System.out.println("Enter K to enter a postfix expression or F to open a text file:"); // requesting user input
String choice = input.nextLine(); // Taking user input from keyboard, either K for manual entry or F for file input
float answer; // Variable used to store answers.
if ("F".equals(choice)) {
System.out.println("Please enter the name of a .txt file:"); // user prompted to enter the name of the text file
String file = input.nextLine(); // File name will be stored in a String (file) to be used later
File txtfile = new File(file);
Scanner reader = new Scanner(txtfile); // Creating a new scanner which reads the text file
while (reader.hasNextLine()) { // The new scanner (reader) reads each line of the text file
String expression = reader.nextLine(); // Each line is stored in a String called Expression
String[] parts = expression.split(" "); // The string is split into three parts; two numbers and an operator
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];
float number1 = Float.parseFloat(part1); // Parts one and two are both converted from a String to a Float
float number2 = Float.parseFloat(part2);
// Program processes the operator and applies it to Float number1 and Float number2 before outputting the
// expression and the correct answer
if ("+".equals(part3)) {
answer = number1 + number2;
System.out.println(part1 + " " + "+" + " " + part2 + " " + "=" + " " + answer);
validExpressions++;
} else if ("-".equals(part3)) {
answer = number1 - number2;
System.out.println(part1 + " " + "-" + " " + part2 + " " + "=" + " " + answer);
validExpressions++;
} else if ("*".equals(part3)) {
answer = number1 * number2;
System.out.println(part1 + " " + "*" + " " + part2 + " " + "=" + " " + answer);
validExpressions++;
} else if ("/".equals(part3)) {
answer = number1 / number2;
System.out.println(part1 + " " + "/" + " " + part2 + " " + "=" + " " + answer);
validExpressions++;
} else{
invalidExcepressions++;
continue;
}
highestval=(highestval>=answer)?highestval:answer;
lowestval=(lowestval<=answer)?lowestval:answer;
total+=answer;
}
} else if ("K".equals(choice)){ // If the user enters K input will be taken from the keyboard
System.out.println("Please enter a post-fix expression:"); // User is prompted to enter a post fix expression
Scanner expression = new Scanner(System.in); // Creating a new Scanner called Expression to read user input
String exp = expression.nextLine(); // The user's input is stored in a string
String[] elements = exp.split(" "); // Expression is split into three elements, two numbers and an operator
String element1 = elements[0];
String element2 = elements[1];
String element3 = elements[2];
float num1 = Float.parseFloat(element1); // Element1 and element2 are converted to Floats and named num1 and num2
float num2 = Float.parseFloat(element2);
// Program processes the operator and applies it to Float num1 and Float num2 before outputting the
// expression and the correct answer
if ("+".equals(element3)) {
answer = num1 + num2;
System.out.println(element1 + " " + "+" + " " + element2 + " " + "=" + " " + answer);
validExpressions++;
} else if ("-".equals(element3)) {
answer = num1 - num2;
System.out.println(element1 + " " + "-" + " " + element2 + " " + "=" + " " + answer);
validExpressions++;
} else if ("*".equals(element3)) {
answer = num1 * num2;
System.out.println(element1 + " " + "*" + " " + element2 + " " + "=" + " " + answer);
validExpressions++;
} else if ("/".equals(element3)) {
answer = num1 / num2;
System.out.println(element1 + " " + "/" + " " + element2 + " " + "=" + " " + answer);
validExpressions++;
}else{
invalidExceotions++;
continue;
}
highestval=(highestval>=answer)?highestval:answer;
lowestval=(lowestval<=answer)?lowestval:answer;
total+=answer;
}else if (choice.isEmpty()) { // If the user does not enter K or F the program will exit
System.out.println("Evaluations Complete");
System.out.println("-------------------------");
System.out.println("Valid expressions: " + validExpressions);
System.out.println("Invalid expressions: " + invalidExpressions);
System.out.println("Total :"+total);
System.out.println("Average :"+total/validExceptions);
System.exit(0);
}
}