呼叫月份名称

问题描述 投票:-1回答:1

我需要帮助来改进我的代码。这只是给我的,我想了解更多。因此,我在课堂上有关于数组的作业。我已经完成编码并提交了,但是我真正想要编码的是该程序打印出诸如“一月的降雨值:156.98”之类的月份名称,而不是“一月”。这是我的代码:首先,我创建了一个没有main方法的类。此类有一个构造函数,有3个方法。

public class RainFall {

    private double[] rainFall;

    public RainFall(double [] r){
        //create an array as large as r
        rainFall = new double [r.length];

        //sopy the elements from r to rainFall
        for (int index = 0 ; index  < r.length ; index ++)
            rainFall[index] = r[index];
    }

    //total method to get the total value of rain in a year
    public double getTotal ()

    double total  = 0.0; //hold the total rain value

    for (int i = 0 ; i < rainFall.length ; i++)    //accumulate the rain value through out a year.
    {
        total += rainFall[i];
    }
    return total;
}
    //get the average value of the rain in a year
    public double getAverage(){

        return getTotal () / rainFall.length;

    }

    //get the month with the most rain
    public double getHighest ()
    {
        double highest = rainFall[0];

        for(int index = 1 ; index < rainFall.length; index ++){
            if(rainFall[index] > highest)
                highest = rainFall[index] ;
        }
        return highest;
    }

    //get the month with the least rain 
     public double getLowest ()
    {
        double lowest = rainFall[0];

        for(int index = 1 ; index < rainFall.length; index ++){
            if(rainFall[index] < lowest)
               lowest = rainFall[index] ;
                if(lowest < 0 )
                {
                  System.out.println("NO NEGATIVE NUMBER");
                  return 0;
                }   

        }
        return lowest;
    }
}

然后我用main方法创建了另一个类来调用其他方法:

import java.util.Scanner;
public class CalculateRain {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        final int MONTHS  = 12; //the 12  months



        //create an array to hold the rain values for a year
        double []  rainFall = new double [MONTHS];


        getValues(rainFall);



        //create a rain fall object
        RainFall re = new RainFall (rainFall);

        //Display the total, average, highest, lowest values of the rain fall in
        // a year
        System.out.println("The total value of rain fall in a year: " + re.getTotal());
        System.out.println("The average value is: " + re.getAverage());
        System.out.println("The highest month has the value: " + re.getHighest());
        System.out.println("The lowest month has the value: " + re.getLowest());

        }

        private static void getValues(double [] array)
        {
            Scanner keyboard = new Scanner (System.in);

            //get the values of the months
            for (int i = 0 ; i  < array.length ; i ++)
            {
                    System.out.println("Enter the value for the month "  + (i+1));
                    array[i] = keyboard.nextDouble();
                    if (array[i] < 0 )
                    {
                        System.out.println("The value SHOULD NOT BE a NEGATIVE number");
                        break;
                    }

            }
    }
}

我试图在for(int i = 0; i

但是在此之前,我创建了另一个数组来保存月份的名称

String  [] month = {"January", "February","March",etc};```

在for循环中,我做了这样的事情

for ( int i = 0 ; i < array.length ; i ++)
{
  for (int index = 0; month.length; index++)
   {
     System.out.println("The rainfall value of the " + month[index]);
       array[i] = keyboard.nextDouble();
                if (array[i] < 0 )
                {
                    System.out.println("The value SHOULD NOT BE a NEGATIVE number");
                    break;
                }
    }
 }

它运行了,但是我创建了一个无限循环。我在哪里想念它?谢谢你们,谢谢。

java arrays reinforcement-learning
1个回答
0
投票

您在第二个for循环中缺少支票。

for (int index = 0; month.length; index++)

条件应该是index < month.length,而不是month.length

© www.soinside.com 2019 - 2024. All rights reserved.