java - 需要帮助从数组中检索最大索引

标签 java

我被卡住了...我的代码之前可以工作,但现在它只是挂起。最重要的是,我似乎无法让我的 getHighest 和 getSmallest 方法返回正确的值。我不知道我是否只是没有捕获一些东西。任何帮助都会很棒!

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

public class Rainfall
{
  public static void main(String[] args)
  {
    final int MONTHS = 12;
    double[] rain = new double[MONTHS];
    initRain(rain);
    double total = totalRain(rain);
    double average = averageRain(rain, total);
    double most = mostRain(rain);
    double least = leastRain(rain);
    DecimalFormat digit = new DecimalFormat("#.0");
    System.out.println("The total rainfall of the year is " + digit.format(total));
    System.out.println("The average rainfall of the year is " + digit.format(average));
    System.out.println("The month with the highest amount of rain is " + most);
    System.out.println("The month with the lowest amount of rain is " + least);
  }
  public static void initRain(double[] array)
  {
    Scanner keyboard = new Scanner(System.in);
    for (int x = 0; x < array.length; x++)
    {  
      System.out.print("Enter Rainfall for month " + (x + 1) + ": ");
      array[x] = keyboard.nextDouble();
    }
  }
  public static double totalRain(double[] array)
  {
    double total = 0;
    for (int x = 0; x < 12; x++)
      total += array[x];
    return total;
  }
  public static double averageRain(double[] array, double total)
  {
    return total / array.length;
  }
  public static double mostRain(double[] array)
  {;
    double maximum = array[1];
    int value = 0;
    for (int i=0; i < 12; i = i++)
    {
      if (array[i] >= maximum)
        maximum = array[i];
        value = i;
      }
    return value;
  }
  public static double leastRain(double[] array)
  {
    double minimum = array[0];
    int value;
    for (int i=0; i < 12; i++)
    {
      if (array[i] <= minimum) 
        minimum = array[i];
        value = i;
      }
    return value;
  }
}

最佳答案

您的程序因这一行而挂起:

for (int i=0; i < 12; i = i++)

问题在于,i++返回的是i的值在变量递增之前,因此增量循环的步骤与编写 i=i 相同。因此,变量i永远不会达到循环的转义条件。

应该是:

for (int i=0; i < 12; i++)

稍微清理一下你的代码,可以改进很多。有很多简单的错误。

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

public class Rainfall
{
  public static void main(String[] args)
  {
    final int MONTHS = 12;
    double[] rain = new double[MONTHS];
    initRain(rain);
    double total = totalRain(rain);
    double average = averageRain(rain, total);
    int most = mostRain(rain);
    int least = leastRain(rain);
    DecimalFormat digit = new DecimalFormat("#.0");
    System.out.println("The total rainfall of the year is " + digit.format(total));
    System.out.println("The average rainfall of the year is " + digit.format(average));
    System.out.println("The month with the highest amount of rain is " + (most + 1));
    System.out.println("The month with the lowest amount of rain is " + (least + 1));
  }
  public static void initRain(double[] array)
  {
    Scanner keyboard = new Scanner(System.in);
    for (int x = 0; x < array.length; x++)
    {
      System.out.print("Enter Rainfall for month " + (x + 1) + ": ");
      array[x] = keyboard.nextDouble();
    }
  }
  public static double totalRain(double[] array)
  {
    double total = 0;
    for (int x = 0; x < 12; x++)
      total += array[x];
    return total;
  }
  public static double averageRain(double[] array, double total)
  {
    return total / array.length;
  }
  public static int mostRain(double[] array)
  {
    double maximum = array[1];
    int value = 0;
    for (int i=0; i < 12; i++) {
      if (array[i] >= maximum) {
        maximum = array[i];
        value = i;
      }
    }
    return value;
  }
  public static int leastRain(double[] array)
  {
    double minimum = array[0];
    int value = 0;
    for (int i=0; i < 12; i++)
    {
      if (array[i] <= minimum) {
        minimum = array[i];
        value = i;
      }
    }
    return value;
  }
}

关于java - 需要帮助从数组中检索最大索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13748189/

相关文章:

Java 8 类型推断导致在调用时忽略泛型类型

java - org.openqa.selenium.WebDriverException : unknown error: cannot determine loading status

java - 如何检测和处理CXF中的写入超时

java - Swing 应用程序中的 Hibernate 和 Spring

java - 将数组分割成不同大小的 block

java - 在java中,每个类如何继承自Object类而不使用extends关键字?

java - Spring Data Jpa - 延迟获取模式不适用于@OneToOne

java - 没有 OutOfMemory 异常的内存泄漏?

java - IRC:无身份响应

java - 使用 RestEasy + servlet 3 自动扫描休息服务