我正在编写一个Java程序,获取每个月的降雨量。它运行得很完美,但我只需要知道如何获取一个月份的索引 - 例如,有一个输出语句:降雨最少的月份是1,降雨量为1.6英寸。如何获取'1',也就是最低月份的索引?我可以很好地获得实际的最低降雨量数据,但无法获得索引。
我尝试过months[n-1],但我仍然收到错误消息“非静态变量months不能从静态上下文中引用”。
任何帮助都会很棒。谢谢。
// 编辑
这是代码。我尝试了一些静态的调整,但那只给我带来了更多的错误?所以我被困在了底部的months[n]部分。
我尝试过months[n-1],但我仍然收到错误消息“非静态变量months不能从静态上下文中引用”。
任何帮助都会很棒。谢谢。
// 编辑
这是代码。我尝试了一些静态的调整,但那只给我带来了更多的错误?所以我被困在了底部的months[n]部分。
import java.util.*;
public class Rainfall {
Scanner in=new Scanner(System.in);
int month=12;
double total=0;
double average;
double months[];
public Rainfall()
{
months=new double[12];
}
public void setMonths()
{
for(int n=1; n<=month; n++ )
{
System.out.print("Enter the rainfall (in inches) for month #"+n+": ");
months[n-1] = in.nextDouble();
//Input Validation - Cannot accept a negative number
while (months[n-1] < 0)
{
System.out.print("Rainfall must be at least 0. Please enter a new value.");
months[n-1] = in.nextDouble();
}
}
}
public double getTotalRainFall()
{
total = 0;
for(int i=0; i<12;i++)
{
total=total+months[i];
}
return total;
}
public double getAverageRainFall()
{
average = total/12;
return average;
}
public double getHighestMonth()
{
double highest=0;
for ( int i = 0; i < 12; i++)
{
if ( months[i] > highest)
{
highest = months[i] ;
}
}
return highest;
}
public double getLowestMonth()
{
double lowest = Double.MAX_VALUE;
for ( int n = 0; n < month; n++)
{
if (months[n] < lowest )
{
lowest = months[n];
}
}
return lowest;
}
public static void main(String[]args)
{
Rainfall r =new Rainfall();
r.setMonths();
System.out.println("The total rainfall for this year is " + r.getTotalRainFall());
System.out.println("The average rainfall for this year is " + r.getAverageRainFall());
System.out.println("The month with the highest amount of rain is " + months[n] + "with" + r.getHighestMonth() "inches");
System.out.println("The month with the lowest amount of rain is " + months[n] "with" + r.getLowestMonth() "inches");
}
/// 编辑 #2 - 好的,所以上面的代码在获取每个月的用户输入时有效。现在我正在尝试设置数组thisYear中的值(即删除用户输入)。计算不再起作用了。我做错了什么?
package Rainfall;
public class Rainfall {
int month = 12;
double total = 0;
double average;
double getRainAt[];
public Rainfall() {
getRainAt = new double[12];
}
double getTotalRain() {
for (int i = 0; i < 12; i++) {
total = total + getRainAt[i];
}
return total;
}
double getAverageRain() {
average = total / 12;
return average;
}
int getHighestMonth() {
int high = 0;
for (int i = 0; i < 12; i++) {
if (getRainAt[i] > getRainAt[high]) {
high = i;
}
}
return high;
}
int getLowestMonth() {
int low = 0;
for (int i = 0; i < 12; i++) {
if (getRainAt[i] < getRainAt[low]) {
low = i;
}
}
return low;
}
public static void main(String[] args) {
// Create an array of rainfall figures.
double thisYear[] = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7,
3.9, 2.6, 2.9, 4.3, 2.4, 3.7 };
int high; // The high month
int low; // The low month
// Create a RainFall object initialized with the figures
// stored in the thisYear array.
Rainfall r = new Rainfall(thisYear);
// Display the statistics.
System.out.println("The total rainfall for this year is " +
r.getTotalRain());
System.out.println("The average rainfall for this year is " +
r.getAverageRain());
high = r.getHighestMonth();
System.out.println("The month with the highest amount of rain " +
"is " + (high+1) + " with " + r.getRainAt(high) +
" inches.");
low = r.getLowestMonth();
System.out.println("The month with the lowest amount of rain " +
"is " + (low+1) + " with " + r.getRainAt(low) +
" inches.");
}
}
months
必须从r
中获取的问题,我已经发布了一个带有工作代码的答案,并提出了一些其他建议。我知道现在已经很晚了,所以如果早期的答案是正确的,可以自由选择接受。 - Ray Toal