将当前值与Java循环中的先前值进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19809352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:21:57  来源:igfitidea点击:

Compare the current value with previous value in a loop in java

javafor-loop

提问by Ali Baker

How do I compare the current value with previous value in for loop in java and determine if they are different?

java - 如何在java中的for循环中将当前值与先前值进行比较并确定它们是否不同?

Here I have for loop that loop every time I click the mouse. Therefore idepends on where I click.

在这里,每次单击鼠标时,我都会循环该循环。因此i取决于我点击的位置。

Here both the current iand the previous iare equal to 3.

这里当前i和前一个i都等于 3。

https://www.dropbox.com/s/55dao9a9r8o4yis/Untitled%20picture%206.png

https://www.dropbox.com/s/55dao9a9r8o4yis/Untitled%20picture%206.png

Here the current iequal to 7 and the previous iequal to 11.

这里当前i等于 7,前一个i等于 11。

https://www.dropbox.com/s/pj38l23ughn6ey9/Untitled%20picture%207.png

https://www.dropbox.com/s/pj38l23ughn6ey9/Untitled%20picture%207.png

I am trying to make ifstatement to compare the two values of previous iwith the current i.

我试图发表if声明来比​​较以前的两个值i与当前的i.

Here is my for loop

这是我的 for 循环

public void mousePressed() {      
    if (state == 0) {            
        for (int i = 0; i < 6; i++) {
            if (mouseX >= cards[i].x && mouseX <= cards[i].x + cards[i].WIDTH && mouseY >= cards[i].y && mouseY <= cards[i].y + cards[i].HEIGHT) {
                cards[i].flip();                     
            }               
        }

采纳答案by pasha701

Start "i" with "1", smth. like:

以“1”开头,“i”。喜欢:

    for (int i = 1; i < 6; i++) {
        if (cards[i].equals(cards[i-1])) {
            cards[i].flip();                     
        }  
    }

回答by Dark Knight

This is what you are looking for

这就是你要找的

int currentValue=0;
    int lastValue=0;

    for(int i=0;i<10;i++){
        currentValue =i;

        if(i>0){
            System.out.println(" Current Value is "+currentValue+" Last Value is "+lastValue);
        }   

        lastValue=currentValue;
    }

回答by Ritu Shikha

 Map<String, String> previousRow = lines.next();
    while (lines.hasNext()) {
        Map<String, String> row = lines.next();

        if (previousRow.get(key).equals(row.get(key)))
        {
            System.out.println("Found Duplicate Product " + row);
        }
        previousRow = row;