Java阵列计算未正确输出



我当前正在从事编程任务,我需要使用我从文件输入的数组并操纵它以显示最高的人口差异和最低差异,这两年都这些发生。我设法获得了最高的人口差异和正确的一年,但是我无法获得最低的成绩。

我应该将其作为输出:

最大的增长是:318.5万:1954年至1955年

最小的增加是:1,881,000年:1966年至1967年

,但我不断得到:

最大的增长是:318.5万:1954年至1955年

最小的增加是:1,881,000年:1952年至1953年

这是我的代码:

import java.util.Scanner;
import java.io.*;
public class AssignmentOne
{
public static void main(String[] args) throws IOException 
{
    // Array Version
    ArrayVersion();
    System.out.println("Finished Array Version of Assignmentn");
}
// Method for ArrayVersion
public static void ArrayVersion() throws IOException
{
    // Year as variables
    int year = 1950;
    // Array to use for Population data
    int[] population =  new int[40];
    // Call the method to get data into array
    population = getDataFromFile("USPopulation.txt");
    if(population == null)
    {
        System.out.println("Error: Population did not load");
        return;
    }
    //Processing Array Elements
    // Calculating the difference 

    // Output Titles
    System.out.println("This is the Simple Array Version of nPopulation Data US");
    System.out.println("nYear tPopulation tDifference");
    System.out.println("");
    // Output Year, Population, Difference
    System.out.printf("%d t%,d,000n", year, population[0]);
    for(int i = 1; i < population.length; i++)
    {
    System.out.printf("%d t%,d,000 t%,d,000 n", ++year, population[i], population[i] - population[i-1]);
    }
    // Calculating the average + displaying it
    double sum = 0;
    double difference = 0;
    double average;
    for(int index = 1; index < population.length; index++)
    {
        difference = population[index] - population[index-1];
        sum += difference;
    }
    average = sum / population.length * 1000;
    System.out.printf("Avg population difference is: %,12.1f thousandn", average);

    // Greatest Population difference + output
    int highest = 0;
    int gtDiff = 0;
    int year1 = 1950;
    for(int k = 1; k < population.length; k++)
    {
        gtDiff = population[k] - population[k-1];
        while(gtDiff > highest)
        {
            year1 = year1 + 1;
            if(gtDiff > highest)
               highest = gtDiff;
        }

    }
    System.out.printf("Greatest increase is:  %,d thousand for yr: %d to %dn",highest, year1 - 1, year1);
    // Lowest Population Difference + output
    int lowest = population[0];
    int lwDiff = 0;
    int year2 = 1950;
    for(int m = 1; m < population.length; m++)
    {
        lwDiff = population[m] - population[m-1];
        while(lwDiff < lowest)
        {
            year2 = year2 + 1;
            if(lwDiff <  lowest )
                lowest = lwDiff;
        }
    }
    System.out.printf("Smallest increase is:  %,d thousand for yr: %d to %dn",lowest, year2 -1, year2);

}
// Method to get data from specified "filename" into an array of ints
public static int[] getDataFromFile(String USPopulation) throws IOException
{
    // Opening file
    File file = new File("C:/Users/cstuser/Documents/USPopulation.txt");
    Scanner inputFile = new Scanner(file);

     int fileSize = 0;
     while(inputFile.hasNext())
     {
         inputFile.nextInt();
         fileSize++; 
     }
     inputFile.close();
     // Now load the integer data into the array named fileData
     // This is your job
     Scanner input2 = new Scanner(file);
     int[] fileData = new int[fileSize];
     for(int i = 0; i < fileSize; i++)
     {
         fileData[i] = input2.nextInt();
     }
     input2.close();
     // return the array fileData
     return fileData;
}
}

如果有人能告诉我我做错了什么,那就太好了。谢谢你!

编辑:这是文件包含的

151868

153982

156393

158956

161884

165069

168088

171187

174149

177135

179979

.....继续

我建议您更改以下代码(对于负差异):

for(int m = 1; m < population.length; m++)
    {
        lwDiff = population[m] - population[m-1];
        if(lwDiff < lowest)
        {
            year2 = 1950 + m;
            lowest = lwDiff;
        }
    }

并对最大进行相同的更改:

for(int k = 1; k < population.length; k++)
    {
        gtDiff = population[k] - population[k-1];
        if(gtDiff > highest)
        {
            year1 = 1950 + k;
            highest = gtDiff;
        }
    }

最新更新