Java 比较for循环中的值
我正在做2个随机卡片生成器。我想知道哪个更高。在下面的示例中,如何将当前值与以前的值进行比较Java 比较for循环中的值,java,loops,Java,Loops,我正在做2个随机卡片生成器。我想知道哪个更高。在下面的示例中,如何将当前值与以前的值进行比较 for (int i = 0; i < 2; i++){ String suit = suits[deck[i] / 13]; String rank = ranks[deck[i] % 13]; System.out.println("You have the " + rank + " of " + suit); } for(int
for (int i = 0; i < 2; i++){
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("You have the " + rank + " of " + suit);
}
for(int i=0;i<2;i++){
绳状西服=西服[甲板[i]/13];
字符串秩=秩[deck[i]%13];
System.out.println(“您拥有”+套装的“+等级+”);
}
您想比较西装和军衔,取两者中较高的一个,对吗?为什么不将前一个值存储在变量中,然后与第二个值进行比较?
String previousValue = null;
for(...) {
String currentValue = ...;
// you can now compare previous with current, watch out for unset (null)
if( previousValue != null && currentValue (?) previousValue ) {...}
//... any further processing
// following is the last line in your loop, so it is set for next loop iteration
previousValue = currentValue;
}