我已经完成了这项任务。但是我的教授不喜欢我的方法。
编写一个 Java 程序,从输入文件中读取任意行数。 输入文件包含一列选手姓名,旁边是一列选手得分。 寻找 读取值的计数 总和 平均分(小数点后2位) 最大值连同相应的名称。 最小值以及相应的名称。
提示:处理读入的每个数据项并继续。不要保存程序中的所有数据。
===========
我使用 2 arraylist 来存储数据,然后按升序对 arraylist 进行排序,然后在排序后的 arraylist 中选择第一个和最后一个数据。 教授不喜欢我处理程序的方式,因为他不希望我消耗这么多 ram,并要求我使用上面提到的提示。
我不确定我应该用什么方法来解决这个问题。任何建议将不胜感激。
这是输入文件的一部分
9290 阿莱巴姆0
9390 davige0
9490 哈萨0
9590 勒克斯特0
9690 拉夫拉0
9790 smithbl0
9890 哈拉斯姆0
9990 afflrj0
90 amosre0
190元0
第290话
3553 菲利尔01
第4553章
...(数千行)
还有我的代码
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");
}
}
最佳答案
听起来他不想让你把内存中的所有东西塞进一个数组里。 对于最小值/最大值,您可以检查值的每一行是否低于/高于当前值,如果是,则相应地更新新的最小值/最大值。 同样跟踪总和和计数,并从中推导出统计平均值。
似乎重点不在于使用数组,至少我是这样理解的
关于Java 最大值和最小值——两种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25562641/