在Java中如何从文本文件中找到最大值和最小值?

时间:2014-05-26 21:02:15

标签: java file max average min

@rayryeng在我最近修改此文件的过程中对我非常有帮助。由于我的问题现在略有改变,我决定创建一个新问题。我有以下代码,我试图根据txt文件中的列表找到我的最大值和最小值。文本文件如下所示:

6

88

77

92

82

84

72

顶部数字不应以总和和平均值计算,这就是我在代码中加入-6和-1的原因(如下所示)。

    package trials;

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

    public class trials2 {

    public static void main(String[] args) throws IOException {
    // Create new Scanner object to read from the keyboard
    Scanner in = new Scanner(System.in);

    // Grab the name of the file
    System.out.println("Please enter the name of your data file: ");
    String fileName = in.next();

    // Access the file
    Scanner fileToRead = new Scanner(new File(fileName));

    // While there is still stuff in the file...
    double sum = -6;
    int numStudents = -1;
    while (fileToRead.hasNext()) { 
         if (fileToRead.hasNextDouble()) {
             numStudents++;
             sum += fileToRead.nextDouble();
         } else {
                    fileToRead.next();
         }   
     }

   {
            fileToRead.close();
        }
   System.out.println("***Welcome to the Exam Statistics Program!!***");
   System.out.println("Minimum = " + Math.min(sum,sum));
   System.out.println("Maximum = " + Math.max(sum,sum));    
   System.out.println("Average score: " + sum/numStudents);
   System.out.println();
   System.out.println("Number of scores by letter grade: ");
   System.out.println();       
   System.out.println("There are " + numStudents + " scores");
        }
    }

我知道总和,总和是错误的,但我需要填写一些东西,以便我记得把它填满。

我已经尝试过搜索这些帖子以及其他许多人的帮助:

How to find min and maxFinding min/max

但我仍然会遇到错误。今天是我第一天做Java,所以我几乎不知道从哪里开始: - /

代码的最终更改

    package trials;

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

public class trials2 {

public static void main(String[] args) throws IOException {
    // Create new Scanner object to read from the keyboard
    Scanner in = new Scanner(System.in);

    // Grab the name of the file
    System.out.println("Please enter the name of your data file: ");
    String fileName = in.next();

    // Access the file
    Scanner fileToRead = new Scanner(new File(fileName));

    // While there is still stuff in the file...
    double sum = -6;
    int numStudents = -1;
    double maxVal = 0, minVal = 0; //NEW
    boolean bFirstTime = true; //NEW
    double currVal; //NEW
    while (fileToRead.hasNext()) { 
        if (fileToRead.hasNextDouble()) {
            numStudents++;
            currVal = fileToRead.nextDouble(); //NEW

            //NEW
            if (bFirstTime) {
                maxVal = currVal;
                minVal = currVal;
                bFirstTime = false;
            } else {
                maxVal = Math.max(maxVal,currVal);
                minVal = Math.min(minVal, currVal);
            }

            sum += currVal;
        } else {
            fileToRead.next();
        }   
    }
   System.out.println("***Welcome to the Exam Statistics Program!!***");
   System.out.println("Minimum = " + minVal);
   System.out.println("Maximum = " + maxVal);   
   System.out.println("Average score: " + sum/numStudents);
   System.out.println();
   System.out.println("Number of scores by letter grade: ");
   System.out.println();       
   System.out.println("There are " + numStudents + " scores");
}
}

3 个答案:

答案 0 :(得分:0)

您可以尝试写入数组列表并使用collections类。

ArrayList<type> list = new ArrayList<type>"();

 while (fileToRead.hasNext()) {
 list.add(fileToRead.nextDouble());
}

 int max = Collections.max(list);
 int min = Collections.min(list);

答案 1 :(得分:0)

如何找到最小值和最大值:

有两个变量。称他们为最小和最大

将min设置为您可以找到的最大数字。

如果min和max是Integer,那么你已经设置了MAX_VALUE和MIN_VALUE。 将max设置为最小的数字。

然后对于您找到的每个号码,执行:

max = Math.max(max, number); min = Math.min(min, number);

答案 2 :(得分:0)

Math.min(x,y)返回x和y的最小值。 Math.max(x,y)返回x和y的最大值。您应该创建两个名为maxVal和minVal的双变量。在循环中,当您获得每个double值时,使用Math.min()和Math.max()将当前double值与maxVal和minVal进行比较。例如:

    // While there is still stuff in the file...
    double sum = -6;
    int numStudents = -1;
    double maxVal, minVal; //NEW
    boolean bFirstTime = true; //NEW
    double currVal; //NEW
    while (fileToRead.hasNext()) { 
        if (fileToRead.hasNextDouble()) {
            numStudents++;
            currVal = fileToRead.nextDouble(); //NEW

            //NEW
            if (bFirstTime) {
                maxVal = currVal;
                minVal = currVal;
                bFirstTime = false;
            } else {
                maxVal = Math.max(maxVal,currVal);
                minVal = Math.min(minVal, currVal)
            }

            sum += currVal;
        } else {
            fileToRead.next();
        }   
    }