Я читаю статистику землетрясения из файла, и мне нужно определить значения Мин и Макс. есть около 831 величины. Я попытался создать локальные переменные double max = Double.MAX_VALUE; double min = Double.MIN_VALUE;
и сравнить их с значениями двойной величины, которые я извлекаю из файла, но когда я возвращаю значение, он просто дает мне самые низкие и самые высокие значения для любого двойного значения. Вот мой код до сих пор.Определение минимальных и максимальных значений из файла
данных из файла, например:
1.6,"Southern California","Wednesday, January 18, 2012 19:19:12 UTC" 1.8,"Southern California","Wednesday, January 18, 2012 19:03:00 UTC" 1.8,"Southern California","Wednesday, January 18, 2012 18:46:53 UTC" 4.7,"Bonin Islands, Japan region","Wednesday, January 18, 2012 18:20:40 UTC" 1.6,"Southern California","Wednesday, January 18, 2012 17:58:07 UTC" 1.0,"Northern California","Wednesday, January 18, 2012 17:48:03 UTC" 5.2,"Santa Cruz Islands","Wednesday, January 18, 2012 17:26:02 UTC"
import java.util.*;
import java.io.*;
public class QuakeStates2
{
public static void main(String[] args) throws IOException
{
double count = 0.0;
double mag = 0.0;
double total = 0.0;
double average = 0.0;
double max = Double.MAX_VALUE;
double min = Double.MIN_VALUE;
String area = null;
String date = null;
Scanner keyboard = new Scanner(System.in); //Setup the Keyboard scanner
System.out.print("Enter the filename: "); // User input for the filename
String filename = keyboard.nextLine(); //Scanner stores the file name as a String Value
File file = new File(filename); //File turns the Scanner input into a file
Scanner inputFile = new Scanner(file); //inputFile holds the file info and Reads up to the comma
while (inputFile.hasNextLine())
{
String line = inputFile.nextLine();
count++;
StringTokenizer str = new StringTokenizer(line);
if (str.hasMoreTokens())
{
mag = Double.parseDouble(str.nextToken(","));
area = str.nextToken();
date = str.nextToken("\\w");
//System.out.println(mag);
//System.out.println(area);
//System.out.println(date);
}
if (mag > max)
{
max = mag;
}
if (mag < min)
{
min = mag;
}
total = mag+total;
average = total/count;
}
inputFile.close();
System.out.println("# of Lines in the file: " + count);
System.out.println("Sum of Magnitudes: " + total);
System.out.println("Average Magnitude: " + average);
System.out.println("Max Magnitude: " + max);
System.out.println("Min Magnitude: " + min);
}
}
Результаты:
Enter the filename: C:\Users\Owner\Desktop\workspace\QuakeStatistics\quakes1.2012.txt # of Lines in the file: 821.0 Sum of Magnitudes: 1747.0000000000007 Average Magnitude: 2.127892813641901 Max Magnitude: 1.7976931348623157E308 Min Magnitude: 4.9E-324
не используют Double.MIN_VALUE его не то, что вы ожидаете от этого – Peter
@Peter: Очень хорошая точка :) –
легко запомнить, если вы допустили ошибку: D – Peter