-1
我有如下的文本文件,它有2個部分PLUS和MINUS和2個問題在每個部分。我想編寫一個程序來解決它們。我附上我的代碼,因爲我無法到達MINUS藥水。我爲Minus得到的結果仍然使用Plus運算符。Java閱讀文件和做不同的功能
math.txt
[Plus]
Question = 0
num1 = 2
num2 = 3
Question = 1
num1 = 4
num2 = 5
[Minus]
Question = 0
num2 = 6
num1 = 5
Question = 1
num2 = 7
num1 = 2
CODE :
BufferedReader in = null;
InputStream fis;
try {
fis = new FileInputStream(file);
in = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = in.readLine()) != null) {
String[] file_Array = line.split(" = ");
if (file_Array[0].equalsIgnoreCase("num1")) {
num1 = file_Array[1];
} else if (file_Array[0].equalsIgnoreCase("num2")) {
num2 = file_Array[1];
int sum = Integer.parseInt(num1) + Integer.parseInt(num2);
System.out.println("Answer :" + sum);
}
else if (file_Array[0].equalsIgnoreCase("[Minus]")) {
if (file_Array[0].equalsIgnoreCase("num2")) {
num2 = file_Array[1];
} else if (file_Array[0].equalsIgnoreCase("num1")) {
num1 = file_Array[1];
int minus = Integer.parseInt(num2) - Integer.parseInt(num1);
System.out.println("Answer :" + minus);
}
}
}
} catch (IOException ex) {
System.out.println("Input file " + file + " not found");
}
感謝你給我的執行 – newbieprogrammer
請批准或downvote的想法答案。 – Ozgen