简体   繁体   English

需要帮助引用数组和数字格式

[英]Need help referencing in an array and with number formatting

I'm so lost on an assignment and need a hand! 我很迷失任务,需要帮助! Basically, I'm suppose to write a code where users can input the number of inches of rain per month for 12 months. 基本上,我假设要编写一个代码,使用户可以输入12个月每月的降雨英寸数。 It will calculate the total inches, average inches, plus point out the month with the most and least rain. 它将计算总英寸数,平均英寸数,并指出降雨最多和最少的月份。 I have the total and average inches under control. 我控制了总英寸和平均英寸。

The most and least rain is where I'm having issues though. 虽然雨量最大,也最少,这是我遇到问题的地方。 I know how to write the code to figure out the highest and least amount of rain fall, but I don't know how to get it to actually say which actual month in the output. 我知道如何编写代码以找出降雨量最多和最少的降雨,但是我不知道如何实际知道输出中的实际月份。 It wants me to list the actual number in the array of 12 months, for instance, 7 for the seventh month. 它要我列出12个月的数组中的实际数字,例如,第七个月为7。

Another issue I'm having is with the Decimal Format. 我遇到的另一个问题是十进制格式。 I've put the import statement at the top, and I put what I thought was the right code in what I thought was the right place in the body, yet when the average is calculated, it still produces a long string of numbers. 我将import语句放在顶部,并将我认为正确的代码放在我认为正确的位置中,但是当计算平均值时,它仍然会产生一长串数字。 ie 12.12121314141, when I want it to display 12.1. 即12.12121314141,当我希望它显示12.1时。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you in advance! 先感谢您! Here's the code: 这是代码:

import java.util.Scanner;
import java.text.DecimalFormat;

public class Rainfall
{
   public static void main (String [] args)
   {
      final int MONTHS = 12;
   double[] rainFall = new double[MONTHS];

   initRain(rainFall);

   DecimalFormat formatter = new DecimalFormat("##0.0");
   System.out.println("The total rainfall for this year is " +      

            totalRain(rainFall));
   System.out.println("The average rainfall for this year is " + 
            averageRain(rainFall));
   System.out.println("The month with the highest amount of rain is " +     
            highest);
   System.out.println("The month with the lowest amount of rain is " +  

            leastRain(rainFall));
        }

   /**
  totalRain method
  @return The total rain fall in the array.
   */

   public static double totalRain(double[] rainFall)
   {
      double total = 0.0;     // Accumulator

  // Accumulate the sum of the elements in the rain fall array
  for (int index = 0; index < rainFall.length; index++)
     //total += rainFall[index];
     total = total + rainFall[index];

  // Return the total.
      return total;
   }

   /**
  averageRain method 
  @return The average rain fall in the array.
   */

   public static double averageRain(double[] rainFall)
   {
      return totalRain(rainFall) / rainFall.length;
   }

   /**
  mostRain method
  @return The most rain in the rain fall array.
   */

   public static double mostRain(double[] rainFall)
   {
  double highest = rainFall[0];

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

  return highest;
   }

   /**
  leastRain method
  @returns The least rain in rain fall array.
   */

   public static double leastRain(double[] rainFall)
   {
  double lowest = rainFall[0];

  for (int index = 0; index < rainFall.length; index++)
  {
     if (rainFall[index] < lowest)
        lowest = rainFall[index];
  }

  return lowest;
   }

      /**
  initRain method 
  @return The rain fall array filled with user input
   */
public static void initRain(double[] array)
{
      double input;  // To hold user input.
  Scanner scan = new Scanner(System.in);

      for (int index = 0; index < array.length; index++)
  {
    System.out.print("Enter rain fall for month " + (index + 1)     

            +": ");
    array[index] = scan.nextDouble();
      }
}
}

For the format issue, make this: 对于格式问题,请执行以下操作:

System.out.println("The total rainfall for this year is " +      
formatter.format(totalRain(rainFall)));

And so on with all return values. 以此类推所有返回值。

I don't get the issue regarding months thou. 关于您的几个月,我没有任何问题。 Could you explain it a little more? 您能再解释一下吗?

edit. 编辑。 Okay, I get it. 知道了 The value of index inside the loop when you assign the higuestRain,... values will be the month you are searching for ; 当您分配higuestRain,...时,循环内的index值将是您要搜索的月份; )

You defined a DecimalFormat but you never use it in your program. 您定义了DecimalFormat,但从未在程序中使用它。 Try 尝试

System.out.println("The average rainfall for this year is " + formatter.format(averageRain(rainFall)));

The Format classes are not global for your application you define them with your formating strings and then use format to pretty print the Number to String or parse to convert a String to Number. 对于您的应用程序,Format类不是全局的,您可以使用格式化字符串定义它们,然后使用format漂亮地将Number打印为String或进行分析以将String转换为Number。

To get the actual month you need to store the index in the array, instead of the value that index contains. 要获取实际月份,您需要将索引存储在数组中,而不是将索引包含的值存储在数组中。 In a nutshell you need to replace 简而言之,您需要更换

highest = rainFall[index];

with

highest = index;

And that'll give you a number that goes from 0 to 11, which are the indexes available in an Array with 12 positions. 这将为您提供一个从0到11的数字,这是具有12个位置的数组中可用的索引。 If you want a number from 1 to 12, to be related to our months you can do this: 如果要将1到12之间的数字与我们的月份相关,可以执行以下操作:

highest = index + 1

And as for the Formatter, you're never using it. 至于Formatter,您永远不会使用它。 Java will not use it implicitly if that's what you thought. 如果您认为那样,Java不会隐式使用它。 You need to explicitly invoke it's methods to get the propper result. 您需要显式调用它的方法以获得适当的结果。 Something like: 就像是:

formatter.format(totalRain(rainFall));

EDIT 编辑

To help you get the idea, here's an implementation of your mostRain method 为了帮助您理解,这里是您的mostRain方法的实现

public static double mostRain(double[] rainFall){
    double highest = rainFall[0];

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

    return highest + 1;
}

For decimal format problem here is solution that you can use. 对于十进制格式问题,这里是您可以使用的解决方案。

import java.text.DecimalFormat; 导入java.text.DecimalFormat; import java.math.RoundingMode; 导入java.math.RoundingMode;

public class Test 公共课测验

{ {

public static void main(String args[]) 公共静态void main(String args [])

{ {

    double i = 12.12121314141;
    DecimalFormat format = new DecimalFormat("#.#"); 
    format.setRoundingMode(RoundingMode.FLOOR);
    String s = format.format(i);
    i = Double.parseDouble(s);
    System.out.println(i); //should be 12.1

} } }}

To get the index you need 2 variables, one to store the amount of rainfall and the other to remember the index (or month): 要获取指数,您需要2个变量,一个变量存储降雨量,另一个变量记住指数(或月份):

public static double mostRain(double[] rainFall)
{
  double highest = rainFall[0];
  int monthIndex;

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

  return monthIndex;
}

The method will return the index of the month with the highest rainfall, you can then use the index to retrieve the actual amount of rainfall from the array. 该方法将返回降雨量最高的月份索引,然后您可以使用该索引从数组中检索实际降雨量。

I know how to write the code to figure out the highest and least amount of rain fall, but I don't know how to get it to actually say which actual month in the output. 我知道如何编写代码以找出降雨量最多和最少的降雨,但是我不知道如何实际知道输出中的实际月份。

You can return the month rather than returning the highest rainfall in that month as follows: 您可以返回该月份,而不是返回该月的最高降雨量,如下所示:

public static int mostRainMonth(double[] rainFall)
{
  double highest = rainFall[0];
  int month = index;
  for (int index = 0; index < rainFall.length; index++)
  {
    if (rainFall[index] > highest)
    {
       highest = rainFall[index];
       month = index;
    }
  }

  return month;
}

You can then use this method in as follows: 然后可以按以下方式使用此方法:

int highestRainMonth = mostRainMonth(rainfall);
System.out.println("The month with the highest amount of rain is " +     
        rainfall[highestRainMonth]+" in the month "+highestRainMonth);

Another issue I'm having is with the Decimal Format. 我遇到的另一个问题是十进制格式。 I've put the import statement at the top, and I put what I thought was the right code in what I thought was the right place in the body, yet when the average is calculated, it still produces a long string of numbers. 我将import语句放在顶部,并将我认为正确的代码放在我认为正确的位置中,但是当计算平均值时,它仍然会产生一长串数字。 ie 12.12121314141, when I want it to display 12.1. 即12.12121314141,当我希望它显示12.1时。

You have just initialized the DecimalFormatter object, but you haven't used it anywhere. 您刚刚初始化了DecimalFormatter对象,但尚未在任何地方使用它。 You can use the setMaximumFractionDigits method as follows: 您可以使用setMaximumFractionDigits方法,如下所示:

DecimalFormat df = new DecimalFormat("##0.0");
System.out.println("The average rainfall for this year is " + 
        formatter.format(averageRain(rainFall)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM