2013-11-26 51 views
0

我越來越沮喪與這一個。我需要從任意文件讀取和寫回的文件在一週的天高,低溫度和平均的那一天......本週天數高低溫平均陣列

1  62 

2  54 

7  55 

6  77 

1  55 

並假定去無限未知。我需要採取該文件,並將其寫入此

dow   high   low   average 
1   78   66   70 

2   87   77   81 

3   70    80   75 

我無法弄清楚我要去哪裏錯了。

import java.io.File; 
import java.util.Formatter; 
import java.util.Scanner; 

public class dowsdowsdows { 

    public static void main(String [] args) { 

     try { 

      Scanner scanner = new Scanner(new File("Dowinputnumbers.txt")); 
      Formatter formatter = new Formatter(new File("Dowoutputnumbers.txt")); 

      int [] dows; 
      int [] hightemps; 
      int [] lowtemps; 
      int [] count = null; 

      while (scanner.hasNext()) { 

       int dow = scanner.nextInt(); 
       int temp = scanner.nextInt(); 
       dows = new int [8]; 

       hightemps = new int [8]; 

       lowtemps = new int [8]; 

       formatter.format("%d %d\n", dows, hightemps, lowtemps); 
       { 
        for (int i = 0 ; i < 7 ; i++) { 
         int j = i + 1; 
         int d = (int) dows[i]/count[i]; 
         formatter.format(j + " " + " " + " " + d); 

        } 

        break; 

       } 

      } 
      formatter.flush(); 
      formatter.close(); 

     } 
     catch (Exception e) { 
     } 
    } 
} 
+0

你爲什麼初始化在'while'循環的每次迭代中的數組? – Maroun

+0

考慮使用一個'Map'來保存ID作爲鍵和一個自定義對象的最大值,最小值和平均值,你可以改變它每次迭代的值爲 –

+0

,只要掃描器有更多的整數,它會繼續? – sobey

回答

0

我看到2件事情讓你的工作更輕鬆。首先,使用容器(因爲你不是寫在純C中,而是使用「特性」Java,它使你的代碼變得簡單)。我會選擇地圖閱讀。併爲寫作結果 - 地圖>。 int - 用於dow,List - 用於獲取信息。 (這是一種糟糕的做法,在'while'循環中一步一步地分配內存)

第二 - 你應該更清楚地理解你想要做什麼。賴特在這個字(英語或俄語或西班牙語)algorythm和後編寫的代碼。

0

您可以計算從文件的最高,最低氣溫與下面的代碼:

鑑於Dowinputnumbers.txt包含:

1 62 
2 54 
7 55 
6 77 
1 55 

然後Temperatures.java:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Formatter; 
import java.util.Scanner; 
import java.util.NoSuchElementException; 

public class Temperatures 
{ 
    public static final int DOWS = 7; 
    public static final int NOVAL = 999; 
    public static final String DI = "Dowinputnumbers.txt"; 
    public static final String DO = "Dowoutputnumbers.txt"; 

    public static boolean isValid(int dow) 
    { 
     return 1 <= dow && 7 >= dow; 
    } 

    public static void main(String[] args) 
    { 
     /** 
     * we need array starting from 1 ([1..DOWS]) 
     * so just use 1 plus size array 
     */ 
     int[] hightemps = new int[DOWS + 1]; 
     int[] lowtemps = new int[DOWS + 1]; 
     int[] counts = new int[DOWS + 1]; 
     int[] sum  = new int[DOWS + 1]; 
     int dow; 
     int temp; 

     /** 
     * init highest and lowest temps to "no value" values 
     */ 
     for (int i = 1; i < DOWS + 1; i++) 
     { 
      hightemps[i] = -NOVAL; 
      lowtemps[i] = NOVAL; 
     } 

     try 
     { 
      Scanner scanner = new Scanner(
        new File(DI)); 
      Formatter formatter = new Formatter(
        new File(DO)); 

      while (scanner.hasNextInt()) 
      { 
       try 
       { 
        dow = scanner.nextInt(); 
        temp = scanner.nextInt(); 
        if (isValid(dow)) 
        { 
         counts[dow]++; 
         sum[dow] += temp; 
         if (hightemps[dow] < temp) 
         { 
          hightemps[dow] = temp; 
         } 
         if (lowtemps[dow] > temp) 
         { 
          lowtemps[dow] = temp; 
         } 
        } 
       } 
       catch (NoSuchElementException e) 
       { 
        /** 
        * input has ended 
        */ 
        System.out.println("No data left?!.. :("); 
       } 
      } 

      /** 
      * now we have collected all temperatures 
      */ 
      formatter.format("dow high low average\n"); 
      for (dow = 0; dow < DOWS; dow++) 
      { 
       /** 
       * if we have temps for dow 
       */ 
       if (0 < counts[dow]) 
       { 
        formatter.format(
          "%d %d %d %.2f\n", 
          dow    , 
          hightemps[dow] , 
          lowtemps[dow] , 
          ((double) sum[dow])/counts[dow]); 
       } 
      } 
      formatter.flush(); 
      formatter.close(); 
     } 
     catch (FileNotFoundException e) 
     { 
      System.out.println("File not found!"); 
     } 
    } 
}