我已經完成了這項任務。但我的教授不喜歡我的方法。
編寫一個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");
}
}
你的方法是什麼? – 2014-08-29 06:18:10
這個問題似乎是脫離主題,因爲它是一個家庭作品,並沒有顯示任何努力。 – 2014-08-29 06:18:30
嘗試發佈一些你試過的代碼 – 2014-08-29 06:20:02