我写了这段代码,因为我想在 7 个数字中连续打印 5 个最小的数字,但不想打印重复的值。现在它给出了一些例外。
该怎么办?
我想我需要一个动态类型,Arraylist
但我不知道完全应用它。
class mycode{
public static void main(String[] args) {
boolean lowStraightFound = false;
int firstCard=0 ;
int firstCardIndex = 0;
int lastCard =0;
int lastCardIndex = 0;
int c[]={1,2,3,3,4,5,7};
int[] inarow = new int[20];
int index[]=new int[5];
int last=0;
int num = c.length;
for (int i = 0; i < num; i++) {
int card = c[i];
if (lastCard!= 0) {
int lastOrd = lastCard;
int cardOrd = card;
if (cardOrd - lastOrd == 1) {
inarow[0]++;
lastCardIndex = i;
last++;
index[last]=i;
} else if (cardOrd - lastOrd != 0) {
inarow[0] = 1;
firstCard = card;
firstCardIndex = i;
last=0;
index[last]=i;
}else if(cardOrd - lastOrd == 0){
index[last]=i;
last=i;
}
} else {
firstCard = card;
firstCardIndex = i;
index[last]=i;
}
lastCard = card;
if (inarow[0] == 5) {
lowStraightFound = true;
break;
}
}
for (int i = last; i >= 0; i--) {
System.out.println(c[i]);
}
}
}