最大和最小的数字

时间:2018-05-14 14:33:56

标签: java sorting max

所以我有一个包含以下值的文件:3,455; 1,67; 83,98; 0,1; 23,178; 2.45; 3.5; 16,88。代码向用户显示值,并且必须确定哪个数字最大和最小。这就是我写的内容,但我得到了一个不同的输出,它应该显示的内容。

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


public class NumbersNew {

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

        //Create a scanner object which will read the data from the file

        Scanner sc = new Scanner(new File("Numbers.txt"));
        sc.useDelimiter("\\s*;\\s*");
        while (sc.hasNextLine()) {

            System.out.println(sc.nextLine());
        }


            //Determine which number was the greatest and which one was the least
        double largest = Double.MIN_VALUE;
        double smallest = Double.MAX_VALUE;

        while(sc.hasNextDouble()) {
            double val = sc.nextDouble();

            if (val < smallest) {
                smallest = val;

            }
            if(val > largest) {
                largest = val;
            }

            System.out.println(largest);
            System.out.println(smallest);

        }

        sc.close();

        //Print these numbers
        System.out.println("The biggest number in the file is: " + largest);
        System.out.println("The smallest number in the file is: " +smallest);
    }
}

这是我得到的输出,我不明白为什么:

3455; 1,67; 83,98; 0,1; 23178; 2.45; 3.5; 16,88 文件中最大的数字是:4.9E-324 文件中最小的数字是:1.7976931348623157E308。

有人可以提出建议或指出我正确的方向吗?谢谢!

0 个答案:

没有答案