Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解释代码(带数组的if语句)_Java_Arrays_If Statement - Fatal编程技术网

Java 解释代码(带数组的if语句)

Java 解释代码(带数组的if语句),java,arrays,if-statement,Java,Arrays,If Statement,此代码打印出阵列在连续两天内的最大温度波动 但我真的不明白,if语句中发生了什么 有人能给我解释一下吗 public class NurTests { public static void main(String[] args) { int[] temperature = { 12, 14, 9, 12, 15, 16, 15, 15, 11, 8, 13, 13, 15, 12 }; int maxTempDiff = 0; int foundDay = 0;

此代码打印出阵列在连续两天内的最大温度波动

但我真的不明白,if语句中发生了什么

有人能给我解释一下吗

public class NurTests {

public static void main(String[] args) {

    int[] temperature = { 12, 14, 9, 12, 15, 16, 15, 15, 11, 8, 13, 13, 15, 12 };

    int maxTempDiff = 0;
    int foundDay = 0;
    for (int i = 0; i < temperature.length; i++) {
        int newMaxDiff = 0;
        if ((i + 1) < temperature.length) {
            if (temperature[i] < temperature[i + 1]) {
                newMaxDiff = temperature[i + 1] - temperature[i];
            }
            if (temperature[i] >= temperature[i + 1]) {
                newMaxDiff = temperature[i] - temperature[i + 1];
            }
            if (maxTempDiff < newMaxDiff) {
                maxTempDiff = newMaxDiff;
                foundDay = i;
            }
        }
    }
}
公共类测试{
公共静态void main(字符串[]args){
int[]温度={12,14,9,12,15,16,15,15,11,8,13,13,15,12};
int-maxTempDiff=0;
int foundDay=0;
对于(int i=0;i=温度[i+1]){
newMaxDiff=温度[i]-温度[i+1];
}
if(maxTempDiff
}


提前谢谢。

我添加了一些评论-应该会有帮助

        // Make sure we don't access beyond the length of the array.
        if ((i + 1) < temperature.length) {
            // Is this temp less than the next one?
            if (temperature[i] < temperature[i + 1]) {
                // New max diff is next minus this.
                newMaxDiff = temperature[i + 1] - temperature[i];
            }
            // Is this temp greater than or equal to the next one?
            if (temperature[i] >= temperature[i + 1]) {
                // New max diff is this minus next.
                newMaxDiff = temperature[i] - temperature[i + 1];
            }
            // Is the new temp diff the greatest so far?
            if (maxTempDiff < newMaxDiff) {
                maxTempDiff = newMaxDiff;
                foundDay = i;
            }
        }
//确保我们的访问不超过数组的长度。
如果((i+1)<温度长度){
//这个温度比下一个低吗?
if(温度[i]<温度[i+1]){
//新的最大差异是下一个减去这个。
newMaxDiff=温度[i+1]-温度[i];
}
//这个温度是否大于或等于下一个温度?
如果(温度[i]>=温度[i+1]){
//新的最大差异是这个减去下一个。
newMaxDiff=温度[i]-温度[i+1];
}
//新的温差是目前为止最大的吗?
if(maxTempDiff
@OldCurmudgeon已经回答了这个问题,但也许你可以利用一些额外的评论:

  • if((i+1)
    可以通过运行循环直到
    i
    :这样
    i+1
    将始终是数组的有效索引,因此无需检查
  • 前两个缩进的
    if
    -s处理温升和温降,对于这两种变化,它们在末尾提供一个正数。Java中有一个数学函数
结合起来:

for (int i = 0; i < temperature.length - 1; i++) {
    int newMaxDiff = Math.abs(temperature[i] - temperature[i + 1]);
    if (maxTempDiff < newMaxDiff) {
        maxTempDiff = newMaxDiff;
        foundDay = i;
    }
}
for(int i=0;i
哪个if语句?所有这些?理解这样的代码的最好方法是使用调试器逐步完成它,并检查每行后面的所有变量。第二个和第三个:)第一个和第二个ifs非常混乱。我将使用绝对差:
newMaxDiff=Math.abs(温度[I+1]-温度[I])