将当前值与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
Compare the current value with previous value in a loop in java
提问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 i
depends on where I click.
在这里,每次单击鼠标时,我都会循环该循环。因此i
取决于我点击的位置。
Here both the current i
and the previous i
are 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 i
equal to 7 and the previous i
equal 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 if
statement to compare the two values of previous i
with 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;