2014-08-29 69 views
1

我已經完成了這項任務。但我的教授不喜歡我的方法。

編寫一個Java程序,可以從輸入文件中讀取任意數量的行。 輸入文件包含一列用於玩家姓名的列,並在其旁邊是每個玩家得分的列。 查找 讀取的值的個數的計數 總和 的平均值(小數點後2位) 最大值隨同相應的名稱。 最小值以及相應的名稱。Java最大和最小值 - 兩種方法

提示:處理讀入的每個數據項並繼續。不要將所有數據保存在程序中。

===========

我沒有使用2的ArrayList來存儲數據,然後以升序排序的數組列表,然後選擇第一個,並在最後一個數據的程序排序arraylist。 這位教授不喜歡我管理這個程序的方式,因爲他不想讓我消耗這麼多的內存並要求我使用上面提到的提示。

我不知道我應該用什麼方法解決問題。任何建議,將不勝感激。 這裏是
9290 alebam0
9390 davige0
9490 hassa0
9590 luxtt0
9690 raflra0
9790 smithbl0
9890 hallasm0
9990 afflrj0
90 amosre0
190 cottat0
輸入文件的一部分290 luzijc0
3553 philel01
4553 poulcp02 ......(千行)

而且有我的代碼

import java.util.*; 
import java.io.*; 
import java.text.DecimalFormat; 

public class GCGC 
{ 
    public static void main(String[] args) throws IOException 
    { 

    ArrayList<String> names = new ArrayList<String>(); 
    ArrayList<Integer> scores = new ArrayList<Integer>(); 
    int nRead = 0;       // hold the number of lines 
    int ListSize;       // hold the size of arraylist    

    final String INPUT_FILE = "/Users/Ali/Desktop/HW1_InputFile.txt"; 
    final String OUTPUT_FILE = "/Users/Ali/Desktop/HW1_Output.txt"; 

    FileWriter fw = new FileWriter(OUTPUT_FILE,false); 
    PrintWriter pw = new PrintWriter(fw); 
    File f = new File(INPUT_FILE); 
    Scanner input = new Scanner(f); 

    // read all data from input line by line 
    while (input.hasNext()) { 
    scores.add(input.nextInt());    
    names.add(input.nextLine().trim()); 
    nRead++; 
    } 

    ListSize = scores.size(); // size of the arraylist would be used throw the program 

    int scoresArray[] = new int [ListSize]; 
    String namesArray [] = new String [ListSize]; 

    // This for loop will convert the arraylist into an array 
    for (int i =0; i<ListSize;i++) 
    { 
    scoresArray[i]=scores.get(i); 
    namesArray[i]=names.get(i); 
    } 

    int theSum = sum (scoresArray); 
    double theAvg = average(scoresArray); 
    outputData(theSum, theAvg, nRead, pw); 
    max_and_min(scoresArray, namesArray, pw); 

    input.close(); 
    pw.close(); 
    System.exit(0); 

    } // end of main 

// ############################################################################# 
// ####################   METHODS   ############################### 
// ############################################################################# 

// This method will find and return the average to the main method 
    public static int sum (int [] scoresArray) 
    { 

    int sum=0; 
    for (int i =0; i < scoresArray.length; i++){ 
    sum+=scoresArray[i];} 
    return sum; 
    } 

// ############################################################################# 
// This method will find and return the average to the main method 
    public static double average (int [] scoresArray) 
    { 

    int sum=0; 
    double avg; 
    for (int i =0; i < scoresArray.length; i++) 
    { 
     sum+=scoresArray[i]; 
    } 
    avg = (double)sum/scoresArray.length ; 

    return avg; 
    } 

// ############################################################################# 
// This method will sort the scores array in an assending order, thus the 
// first element of the array will represnet the minimum and the last element of 
// the array will represent the maximum. 
    public static void max_and_min(int [] score, String [] name, PrintWriter pw) 
    { 

    int tempNum; String tempName; 
    boolean fixed = false; // fixed is true once the array is sorted 

    while (fixed ==false) 
    { fixed = true;  // ture to exit the while loop once the array is fixed 
     for (int i =0 ; i<score.length-1 ; i++) 
     { 
     if (score[i] > score[i+1]) 
     { 
     tempNum = score [i+1]; score [i+1] = score[i]; score[i] = tempNum; 
     tempName = name [i+1]; name [i+1] = name[i]; name[i] = tempName; 

     fixed = false; // Once we are inside the if statment, that 
          //means the array is still not fixed 
     }  
     } 
    } 

    pw.println("The maximum score is: "+score[score.length-1]+" belongs to: " 
    +name[score.length-1]+"\n\n"); 

    pw.println("The Minimum score is: " + score[0] + " belongs to: "+name[0] +"\n\n"); 

    } 

// ############################################################################# 
// This method is for outputting the report to a text file 
    public static void outputData(int theSum, double theAvg, int nRead, PrintWriter pw) 
    { 

    // DecimalFormat is to format the average 
    DecimalFormat f = new DecimalFormat("#0.##"); 

    pw.println("\t\t GCGC Statistical Report"); 
    pw.println("###################################################################"); 
    pw.println("\n\n"); 
    pw.println("The number of read values is: " + nRead + "\n\n"); 
    pw.println("The total Sum is: " + theSum + "\n\n"); 
    pw.println("The average Score is: " + f.format(theAvg) + "\n\n"); 

    } 
} 
+0

你的方法是什麼? – 2014-08-29 06:18:10

+1

這個問題似乎是脫離主題,因爲它是一個家庭作品,並沒有顯示任何努力。 – 2014-08-29 06:18:30

+1

嘗試發佈一些你試過的代碼 – 2014-08-29 06:20:02

回答

0

當你的老師說,你不應該使用的ArrayList,對於每個值只是簡單的變量找到。根據需要更新變量,直到獲得最終結果。提示:您將需要一個計數器來計算總讀取行數,一個變量用於存儲總和,使用前兩個得到平均得分,一個變量保持最大值,另一個保留相應的值名字和另一對夫婦保持最小值和相應的名字。

+1

你不是在給他餵食嗎? – Sufian 2014-08-29 06:28:55

+0

@Sufian xD至少他發佈了自己的代碼... – ChusZ 2014-08-29 06:31:52

+1

計算機科學更多的是*解決問題的能力*而不是*編碼*。 – Sufian 2014-08-29 06:36:52

3

聽起來像他不希望你在數組中的所有內存。 對於最小/最大值,您可以檢查每行的值是否低於/高於當前值,如果是,則相應地更新新的最小/最大值。 同樣從這些跟蹤和計算並推導出統計平均值。

好像整點不使用數組,至少這是我如何解釋它

2

你的教授是問基本上什麼,是你閱讀每個單獨的線並進行處理,增加行數,將比分運行總量和評估如果分數比您讀過任何其他分數較高或較低的...

例如...

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.text.NumberFormat; 

public class ReadScores { 

    public static void main(String[] args) { 
     try (BufferedReader br = new BufferedReader(new FileReader(new File("Scores.txt")))) { 

      int count = 0; 
      int tally = 0; 
      int highest = 0; 
      int lowest = Integer.MAX_VALUE; 

      String highestPlayerName = null; 
      String lowestPlayerName = null; 

      String text = null; 
      while ((text = br.readLine()) != null) { 
       count++; 
       String[] parts = text.split(" "); 
       int score = Integer.parseInt(parts[0]); 
       tally += score; 
       if (score > highest) { 
        highest = score; 
        highestPlayerName = parts[1]; 
       } else if (score < lowest) { 
        lowest = score; 
        lowestPlayerName = parts[1]; 
       } 
      } 

      System.out.println("Number of entries = " + count); 
      System.out.println("Sum of scores = " + tally); 
      System.out.println("Average score = " + NumberFormat.getNumberInstance().format(tally/(double)count)); 
      System.out.println("Highest score of " + highest + " by " + highestPlayerName); 
      System.out.println("Lowest score of " + lowest + " by " + lowestPlayerName); 

     } catch (IOException exp) { 
      exp.printStackTrace(); 
     } 
    } 

} 

了基於該數據...

9290 alebam0 
9390 davige0 
9490 hassa0 
9590 luxtt0 
9690 raflra0 
9790 smithbl0 
9890 hallasm0 
9990 afflrj0 
90 amosre0 
190 cottat0 
290 luzijc0 
3553 philel01 

輸出...

Number of entries = 12 
Sum of scores = 81243 
Average score = 6,770.25 
Highest score of 9990 by afflrj0 
Lowest score of 90 by amosre0 

這樣你只持有在內存中的信息當前行,連同數據一起你爲了提供彙總需要

0

QUESTION VARIABLES

Find the count of the number of values read int count

the total sum int sum

the average score double avgScore

the maximum value along with the corresponding name int max, String maxName

the minimum value along with the corresponding name int min, String minName

Intermediate variables int currentScore,String currentName

現在分析你輸入的文件,並在每個迭代(每行),做到以下幾點: -

1)count++

2)指定當前比分currentScore,目前的球員姓名currentName

3)sum+=currentScore

4)檢查和maxmincurrentScore比較的數值,然後更新它們作爲如果更新maxmin,則還需要更新maxNameminName以及currentName

5)最後經過迭代結束,avgScore=(double)sum/count;

通過這種方式,你將有所有值,而不存儲所有不必要的數據到你的程序。請指出我是否遺漏了一些東西。