在我的java程序中,我需要'回答'的最高和最低值。从文件输入部分,并将它们显示回控制台。我知道使用math.min / math.max命令但无法看到如何将它们容纳到我的代码中?
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class Assignment {
private static Scanner input;
public static ArrayList<String> validList = new ArrayList<String>();
public static boolean isInt(String userinput) {
try {
Integer.parseInt(userinput); // Try to parse. Makes sure that the values entered are actual numbers
return true; // Boolean value to show if the equation entered is valid or not
}
catch (NumberFormatException e) {
System.out.println("Please enter a valid expression!");
return false;
}
}
public static boolean isValidLine(String line) {
line = line.trim();
if (line.length() <= 4) { // Trims the lines down to 4 and ensures there is no spaces being included
return false;
}
else
{
String[] calcarray = new String[3];
calcarray = line.split(" ");
String operators = new String("[+\\-\\*\\/]"); // Validator using regular expressions to check the operator used
if (isInt(calcarray[0].toString()) && isInt(calcarray[1].toString()) && calcarray[2].matches(operators)) { // Checks that the operator in the string matches the ones in the regular expression
return true;
}
else
{
return false;
}
}
}
static void display(String msg)
{
System.out.println(msg);
}
static void processInput() throws IOException
{
String keyboardInput = new String();
Scanner kbScan = new Scanner(System.in);
int answer = 0;
while (true){
display("Please enter an equation");
keyboardInput = kbScan.nextLine();
if (isValidLine(keyboardInput)) {
String[] equation = new String[3]; // We know that this is only going to contain 3 to be valid
equation = keyboardInput.split(" "); // split this up, as it's stored with the spaces.
int num1 = Integer.parseInt(equation[0]);
int num2 = Integer.parseInt(equation[1]);
switch(equation[2]) { // This case switch checks the third position of the string to decide which operator is being used. It then works out the answer and breaks to the next instruction
case("+"):
answer = num1 + num2;
break;
case("-"):
answer = num1 - num2;
break;
case("/"):
answer = num1 / num2;
break;
case("*"):
answer = num1 * num2;
break;
}
display("Your post fix expression: " + equation[0] + " " + equation[1] + " " + equation[2]);
display("Your calculation: " + equation[0] + " " + equation[2] + " " + equation[1] + " = " + answer);
}
else
{
display("The equation you entered is invalid");
}
}
}
static void fileInput() throws IOException
{
input = new Scanner(System.in);
try
{
String currentLine = new String();
int answer = 0;
//Open the file
display("Enter File Name: ");
FileInputStream fstream = new FileInputStream(input.nextLine()); // make a input stream
BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); // pass input stream to a buffered reader for manipulation
String strLine; // create string vars
//loop to read the file line by line
while ((strLine = br.readLine()) != null) { // Whilst the buffered readers read line method is not null, read and validate it.
currentLine = strLine;
if(strLine.trim().isEmpty())
{
display("You have entered a blank line, the program will now exit");
System.exit(0);
}
if(isValidLine(currentLine))
{
String[] filearray = new String[3];
filearray = currentLine.split(" ");
int val1 = Integer.parseInt(filearray[0]);
int val2 = Integer.parseInt(filearray[1]);
display("Your expression is: " + filearray[0] + " " + filearray[1] + " " + filearray[2]);
switch(filearray[2]) {
case("+"):
answer = val1 + val2;
break;
case("-"):
answer = val1 - val2;
break;
case("/"):
answer = val1 / val2;
break;
case("*"):
answer = val1 * val2;
break;
}
display("Your calculation is " + filearray[0] + " " + filearray[2] + " " + filearray[1] + " = " + answer);
}
}
}
catch (FileNotFoundException e)
{
display("Please Enter a valid file name");
}
}
public static void main(String[] args) throws IOException {
while(true)
{
display("Enter F for file calculator or K for keyboard input");
Scanner optionScan = new Scanner (System.in);
String optionInput = optionScan.nextLine();
char letterInput = optionInput.charAt(0);
switch (Character.toUpperCase(letterInput))
{
case 'K':
processInput();
break;
case 'F':
fileInput();
break;
case 'Q':
System.exit(0);
}
}
}
}
答案 0 :(得分:0)
我会使用2 private static
个字段来跟踪你的最小和最大答案。首先,我将与您的其他字段一起创建2个新字段。我给了他们最大整数和最小整数值,以便以后很容易进行比较。
private static Scanner input;
public static ArrayList<String> validList = new ArrayList<String>();
private static int minAnswer = Integer.MAX_VALUE;
private static int maxAnswer = Integer.MIN_VALUE;
然后在您的fileInput
方法中,在您进行计算之后,您可以在此处使用Math.max
和Math.min
。像这样:
display("Your calculation is " + filearray[0] + " " + filearray[2] + " " + filearray[1] + " = " + answer);
minAnswer = Math.min(answer, minAnswer);
maxAnswer = Math.max(answer, maxAnswer);
用户输入空白行后,您将在这些字段中获得最大和最小答案。希望这有帮助!
编辑:我创建了字段,因为我认为您需要访问main方法中的最小值/最大值。如果您只需要在fileInput
方法中访问这些值(在完成所有计算后打印值,然后忘记它们),那么我只需要创建2 int
local 变量而不是fileInput
方法。