Java数组计算输出不正确?

时间:2017-09-11 14:53:20

标签: java arrays output

我目前正在编写一个编程任务,我需要使用从文件中输入的数组并对其进行操作以显示最高的人口差异以及最低的差异,两者都会发生这两年。我设法获得了最高的人口差异和年份正确,但是我无法获得最低的输出正确的年份。

我应该将此作为输出:

最大的增长是:3,185千年:1954年至1955年

最小的增幅是:1881万年:1966年至1967年

但我一直这样做:

最大的增长是:3,185千年:1954年至1955年

最小的增幅为:1881千年: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 Assignment\n");

}

// 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,000\n", 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 thousand\n", 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 %d\n",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 %d\n",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

.....继续

1 个答案:

答案 0 :(得分:0)

我建议你改变你的代码如下(对于减去差异):

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

并对max:

进行相同的更改
for(int k = 1; k < population.length; k++)
    {
        gtDiff = population[k] - population[k-1];

        if(gtDiff > highest)
        {
            year1 = 1950 + k;
            highest = gtDiff;
        }

    }