2015-04-07 23 views
-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"); 
     } 

回答

-1

我的解決方案如下:

public static void main(String arg[]) { 
    BufferedReader in = null; 
    InputStream fis; 
    String file = "math.txt"; 
    try { 
     fis = new FileInputStream(file); 
     in = new BufferedReader(new InputStreamReader(fis)); 
     String line = null; 
     String section=null; 
     String num1 = null; 
     String num2 = null; 
     while ((line = in.readLine()) != null) { 
      String[] file_Array = line.split(" = "); 
      if (file_Array[0].equalsIgnoreCase("[Minus]")) 
      { 
       section="Minus"; 
      } 
      if (file_Array[0].equalsIgnoreCase("[Plus]")) 
      { 
       section="Plus"; 
      } 


      if (file_Array[0].equalsIgnoreCase("num1")) { 
       num1 = file_Array[1]; 
      } else if (file_Array[0].equalsIgnoreCase("num2")) { 
       num2 = file_Array[1]; 
      } 
      //Solution depends on the fact that there will be a blank line 
      //after operands. 
      if (file_Array[0].equals("")){ 
       printResult(section,num1,num2); 
      } 
     } 
     //There is no blank line at the end of the file, so call printResult again. 
     printResult(section,num1,num2); 
    } catch (IOException ex) { 
     System.out.println("Input file " + file + " not found"); 
    } 
} 

private static void printResult(String section,String num1,String num2) { 
    if (section.equals("Minus")){ 
     int minus = Integer.parseInt(num2) - Integer.parseInt(num1); 
     System.out.println("Answer :" + minus); 
    } 
    if (section.equals("Plus")){ 
     int sum = Integer.parseInt(num1) + Integer.parseInt(num2); 
     System.out.println("Answer :" + sum); 
    } 

} 
+0

感謝你給我的執行 – newbieprogrammer

+0

請批准或downvote的想法答案。 – Ozgen