简体   繁体   English

有人可以帮我解决此代码,我是Java新手

[英]Could someone please help me fix this code, I'm new to java

I have a specified array. 我有一个指定的数组。 I need to pass them all through the while loop and everytime there is a change in increasing or decreasing order, I add 1 to Totalrun. 我需要将它们全部传递给while循环,并且每次递增或递减顺序发生变化时,我都向Totalrun加1。 eg 2,3,7 are all increasing, but if the next number in the array is less than 7, then it adds 1 to the run, to start a new run. 例如,2,3,7都在增加,但是如果数组中的下一个数字小于7,则它将1加到运行中以开始新的运行。 A run is a list of contiguous numbers that are either all increasing (ups) or all decreasing (downs). 连续运行是一个连续数字的列表,这些数字要么全部递增(向上),要么全部递减(向下)。

public class run {

    public static void main(String[] args) {

        boolean prevgrad;

        int[] number = {2, 3, 7, 4, 5, 1, 12, 14, 9, 28};

        int Totalrun = 1;

        for (int i = 0; i < number.length - 2; i++) {
            int prevnum = number[i];
            int currnum = number[i + 1];

            if (currnum > prevnum) {
                prevgrad = true;

                if (currnum > prevnum) {
                    if (prevgrad = true) {
                    } else {
                        Totalrun = Totalrun + 1;
                        prevgrad = false;
                        if (currnum < prevnum) {
                            if (prevgrad = false) {
                            } else {
                                Totalrun = Totalrun + 1;
                            }
                            prevgrad = false;
                            break;
                            System.out.println(Totalrun);
                        }
                    }
                }
            }
        }
    }
}

error: 错误:

run.java:30: error: unreachable statement
                        System.out.println(Totalrun);
                        ^
1 error

I think I got it, here it is not for you to copy, but to learn from. 我想我明白了,这里不是您要复制的,而是要学习的。 From here on out, I would HIGHLY recommend you go and watch some videos or even read a good book on Java. 从现在开始,我强烈建议您去看一些视频,甚至读一本有关Java的好书。 Posting every question you have on stackoverflow doesn't help near as much as a good book on programming. 发布关于stackoverflow的每个问题对一本好的编程书籍都没有多大帮助。

public class Test {

    public static void main(String args[]) {
        int[] number = {2,3,7,4,5,1,12,14,9,28}; // 1, 4, -3, 1, -4, 11, 2, -5, 1 <- These are differences from curr - prev

        int totalRun, incRun = 0, decRun = 0; // Not 1, what if theyre all increasing/decreasing? Run never goes to 1.

        for (int i = 0; i < number.length - 1; i++) {
            int prevnum = number[i];
            int currnum = number[i+1];
            int difference = currnum - prevnum;
            if (difference > 0)
                incRun++;
            else
                decRun++;
        }

        if (number[1] > number[0])
            totalRun = incRun;
        else
            totalRun = decRun;

        System.out.println("Total run: " + totalRun);
    }
}

You can't access an array's values in that fashion (at least not that I know of). 您不能以这种方式(至少不是我所知道的)访问数组的值。 There are multiple ways you could use, I personally would use an array for loop for this example. 您可以使用多种方式,在此示例中,我个人将使用数组for循环。

prevNum = 0;
for (int currNum: Number) {
    if (prevNum > currNum) {
        prevgrad = "up";
        ... rest of the code;
    }
}

The reason for this is in this type of for loop, the value of currNum is equal to whatever the value of the array is in that iteration. 原因是在这种for循环中, currNum的值等于该迭代中数组的值。

The complete code would be: 完整的代码为:

int prevNum = 0;
int totalRun = 0;
for (int currNum: Number) {
    if (prevNum > currNum) {
        prevgrad = "up";
    }
    else {
        totalRun++;
        prevgrad = "down";
    }
    prevNum = currNum;
}

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

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