如果数组的索引等于某个数字或超过该数字,我将如何保存它?

时间:2018-11-10 04:56:06

标签: java

我目前正处在一个计算程序的中间,在该程序中,必须计算并打印给定年份范围内的飓风数据。我知道如何将所有信息放入数组中,但是我不知道如何从用户输入中获得一定范围的年份。

例如,在1995-2000年的用户类型中,我可以说第一年是1995年,第二年是2000年,我必须找到这两个数字之间的所有值。我正在寻找1995-2015范围内的任何地方。

我的主要问题来自第二和第三while语句。如果要打印它们的结果,则它们都等于零。我以为这意味着循环根本不会运行,但是我在做什么错呢?

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

public class HurricaneSelector
{
    public static void main (String []args)throws IOException{
        File fileName = new File("hurricanedata.txt");
        Scanner in = new Scanner(System.in);
        Scanner inFile = new Scanner(fileName);
        PrintWriter outFile = new PrintWriter(new File("hurricaneanalysis.txt"));

        int j = 0;
        int numLines = 1000;
        int totalTokens = 0;
        String countLine = " ";
        boolean isFirstYear = false;
        boolean isSecondYear = false;
        boolean notSecondYear = false;
        int firstYearCounter = 0;
        int secondYearCounter = 0;
        double minimum = Double.MAX_VALUE;
        double maximum = Double.MIN_VALUE;        
        double categoryAverage = 0;
        int categoryMin = Integer.MAX_VALUE;
        int categoryMax = Integer.MIN_VALUE;
        double pressureAverage = 0;
        double pressureMin = Double.MAX_VALUE;
        double pressureMax = Double.MIN_VALUE;
        double windAverage = 0;
        double windMin = Double.MAX_VALUE;
        double windMax = Double.MIN_VALUE;
        int category1 = 0;
        int category2 = 0;
        int category3 = 0;
        int category4 = 0;
        int category5 = 0;

        int[] year = new int[numLines];
        String[] month = new String[numLines];
        int[] pressure = new int[numLines];
        double[] windSpeed = new double[numLines];
        String[] name = new String[numLines];
        int[] category = new int[numLines];

        File fileName2 = new File("hurricanedata.txt");
        Scanner inFile2 = new Scanner(fileName2);

        while (inFile2.hasNext()){
            year[j] = inFile2.nextInt();
            month[j] = inFile2.next();
            pressure[j] = inFile2.nextInt();
            windSpeed[j] = inFile2.nextInt();
            windSpeed[j]*=1.15078;
            name[j] = inFile2.next();

            if(windSpeed[j] > 74 && windSpeed[j] < 96){
                category[j] = 1;
            }  else if(windSpeed[j] > 96 && windSpeed[j] < 110){
                category[j] = 2;
            }  else if(windSpeed[j] > 111 && windSpeed[j] < 129){
                category[j] = 3;
            }  else if(windSpeed[j] > 130 && windSpeed[j] < 156){
                category[j] = 4;
            }   else{
                category[j] = 5;
            }               
            j++;
        }


        System.out.println("Please type a range of years from 1995-2015 using this format (XXXX-XXXX)");
        String userInput = in.next();
        String firstYear = userInput.substring(0, 4);
        String secondYear = userInput.substring(5);
        int firstYearInt = Integer.parseInt(firstYear);
        int secondYearInt = Integer.parseInt(secondYear);

        while(isFirstYear = false){
            if(firstYearInt == (year[firstYearCounter])){
                isFirstYear = true;
            }else {
                firstYearCounter++;
            }
        }
        while(isSecondYear = false){
            if(secondYearInt > (year[secondYearCounter])){
                isSecondYear = true;
            } else{
                secondYearCounter++;
            }
        }    

        System.out.println(firstYearCounter);
        System.out.println(secondYearCounter);
        firstYearCounter-=1;
        secondYearCounter-=1;
        totalTokens = secondYearCounter - firstYearCounter;

        for (int i = firstYearCounter; i < secondYearCounter; i++) {
            pressureAverage+=pressure[i];
            if(category[i] == 1){
                categoryAverage+=category[i];
                category1++;
            }  else if(category[i] == 2){
                categoryAverage+=category[i];
                category2++;
            }  else if(category[i] == 3){
                categoryAverage+=category[i];        
                category3++;
            }  else if(category[i] == 4){
                categoryAverage+=category[i];                
                category4++;
            }   else{
                categoryAverage+=category[i];               
                category5++;
            }               
        } 

        for (int i = firstYearCounter; i < secondYearCounter; i++) {
            if (pressure[i] > pressureMax) {
                pressureMax = pressure[i];
            }
            if (pressure[i] < pressureMin) {
                pressureMin = pressure[i];
            }
            if (windSpeed[i] < windMin) {
                windMin = windSpeed[i];
            }
            if (windSpeed[i] > windMax) {
                windMax = windSpeed[i];
            }
            if (category[i] > categoryMax) {
                categoryMax = category[i];
            }
            if (category[i] < categoryMin) {
                categoryMin = category[i];
            }
        }
        categoryAverage = categoryAverage / totalTokens;
        pressureAverage = pressureAverage / totalTokens;
        windAverage = windAverage / totalTokens;

        System.out.println("                      Hurricanes " + firstYear + " - " + secondYear);
        System.out.println();
        System.out.println("Year     Hurricane    Category     Pressure (mb)     Wind Speed (mph)");
        System.out.println("=====================================================================");
        for (int i = firstYearCounter; i < secondYearCounter; i++) {
            System.out.printf("%d%13s%10d%16d%18d\n" , year[i] , name[i] , category[i] , pressure[i] , (int)windSpeed[i]);
            outFile.printf("%d%13s%10d%16d%18d\n" , year[i] , name[i] , category[i] , pressure[i] , (int)windSpeed[i]);
        }


         System.out.println("=====================================================================");
        System.out.printf("%s%20f%16f%18f\n" , "Average" , categoryAverage , pressureAverage , windAverage);
        System.out.printf("%s%20d%16f%18f\n" , "Maximum" , categoryMax , pressureMax , windMax);
        System.out.printf("%s%20d%16f%18f\n" , "Minimum" , categoryMin , pressureMin , windMin);
        System.out.println();
        System.out.println("Number of Category 1: " + category1);
        System.out.println("Number of Category 2: " + category2);
        System.out.println("Number of Category 3: " + category3);
        System.out.println("Number of Category 4: " + category4);
        System.out.println("Number of Category 5: " + category5);
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

这里

while(isFirstYear = false)

=false分配给isFirstYear,您的循环总会得出false。比较就是这样

while(isFirstYear == false)

您在第二个循环中犯了同样的错误。


如果愿意,您可以缩短代码

while(!isFirstYear)