【问题标题】:What am I doing wrong with my while loop?我的while循环做错了什么?
【发布时间】:2016-01-27 10:16:15
【问题描述】:

我正在开发一个程序,该程序接受用户输入(温度)并将其放入数组中。我对必须创建的最后一种方法感到困惑。我需要遍历数组并从最小到最大打印它们。我需要使用 while 循环来执行此操作。

问题是我需要索引与温度值保持一致。该指数代表测量温度的日期。我已经有一种方法可以找到数组中的最小值并将其与索引一起打印出来。

我可以只使用我已有的方法并在新方法中使用它吗?如果是这样,我不知道如何在执行 while 循环时调用该方法。我要尝试做的是找到最小值并打印值和索引,然后将值更改为我的“未初始化”变量,以便我可以找到下一个最小值等等。

我的代码“LeastToGreatest”中的最后一个方法是我尝试这样做,但它不起作用。几个小时以来,我一直在努力尝试自己解决这个问题。我不知道我需要做什么,也不知道我需要如何组织它才能让它工作。

这是我所拥有的:

public class Weather {

static int lowestTemp;
static int lowestDay;

private static final int Uninitialized = -999;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    int[] high = new int[32];


            FindLowestTempInArray(low);
    System.out.println("\n" + "The lowest low is: " + lowestTemp + " degrees." + "\n"
            + "This temperature was recorded on day: " + lowestDay);



            LeastToGreatest(low);
    System.out.println("\n" + lowestDay + "    " + lowestTemp + "\n");


    }


 public static int FindLowestTempInArray(int[] T) {
    // Returns the index of the lowest temperature in array T

    lowestTemp = Uninitialized;
    lowestDay = 0;

    for (int day = 0; day < T.length; day++) {
        if (T[day] != Uninitialized && (T[day] < lowestTemp || lowestTemp == Uninitialized)) {

            lowestTemp = T[day];
            lowestDay = day;
        }
        Arrays.asList(T).indexOf(lowestDay);
    }
    return lowestDay;

}



public static void LeastToGreatest(int[] T) {
    lowestTemp = Uninitialized;
    lowestDay = 0;

    while (lowestDay >= 0 && lowestDay <= 31) {
        for (int day = 0; day < T.length; day++) {

            if (T[day] != Uninitialized && (T[day] < lowestTemp || lowestTemp == Uninitialized)) {

                lowestTemp = T[day];
                lowestDay = day;

            }
        }

    }

    }

}

【问题讨论】:

  • 如果我这样做,我会将温度和天数存储为数组中的两列,因此数组将是 2x31 的 int。然后我会按温度场对其进行排序。最终值将是最高的,并且简单地输出数组将按温度顺序

标签: java arrays for-loop methods while-loop


【解决方案1】:

是的,您可以在此处重复使用其他方法。

public static void leastToGreates(int[] temps) {
    // copying the old array temps into newTempArr
    int[] newTempArr = new int[temps.length];
    for (int i = 0; i < temps.length; i++)
        newTempArr[i] = temps[i];

    int days = 0;
    while (days < temps.length) {
        int lowest = FindLowestTempInArray(newTempArr);
        if (newTempArr[lowest] > Uninitialized)
            System.out.println("temp: " + newTempArr[lowest] + ", on day: " + lowest);
        // setting the temperature of the current lowest day to "Uninitialized"
        // (so it's not the lowest temperature anymore)
        newTempArr[lowest] = Uninitialized;
        days++;
    }
}

我在这里做的是:

  • 复制温度数组(以便能够更改其中的值)而不影响原始数组
  • 打印数组中的最低温度
  • 将该索引的温度设置为“未初始化”
  • 重复

【讨论】:

  • 为什么打印出“temp: 2147483647, on day: 4” 30 次?
  • 我只能猜测,但我认为这是因为您在其他方法中使用了lowestTemperature...这可能仅在所有日期都设置为有效温度时才有效(而不是 -第999章)
  • 新代码完美运行!感谢您向我展示如何做到这一点!
猜你喜欢
  • 2021-02-15
  • 2014-03-24
  • 2018-07-26
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
  • 2014-02-06
  • 1970-01-01
相关资源
最近更新 更多