基本上这个游戏要求输入1到52之间的整数,一旦程序运行,它将输出卡片名称和花色,它适用于除52之外的每个整数,这会导致程序崩溃。有没有人有任何想法?排列在纸牌游戏中的问题
public static void main(String[] args) {
int i = 0;
int[] cards = new int[54];
boolean notZero = true;
while (notZero == true) {
System.out.println("Please input a number in the range 1-52: ");
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
if (input == 0) {
break;
} else if ((input < 1) || (input > 52)) {
System.out.println("Invalid input please try again.");
} else {
cards[i] = input;
}
i++;
}
outputCards(cards, i);
}
public static void outputCards(int[] cards, int arraySize) {
String[] suit = {"Hearts", "Diamonds", "Clubs", "Spades"};
String[] rank = {" ", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10","Jack", "Queen", "King",
"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King",
"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King",
"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
for (int j = 0; j < arraySize; j++) {
System.out.println(rank[cards[j]] + " of " + suit[(int) (cards[j]/13)]);
}
}
当'cards [j] == 52',然后是'52/13 - > 4',这是超过你的西装阵列的末尾,使'suit [4]'你的问题 – 2014-11-24 16:47:23