作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一个尝试学习 Java 的新手,但在完成给自己的任务时遇到了一些困难。基本上我正在尝试做本文末尾的练习page.
我成功地完成了三节课。 卡片:
public class Card {
public int nRank; // Used later
public int maxRank = 13; //The max number of Ranks
public int nSuit; // Used later
public int maxSuit = 4; // Max number of suits
//Associate both rank and suit numbers with strings
public String[] ranks = new String[maxRank - 1];
{
ranks[0] = "two";
ranks[1] = "three";
ranks[2] = "four";
ranks[3] = "five";
ranks[4] = "six";
ranks[5] = "seven";
ranks[6] = "eight";
ranks[7] = "nine";
ranks[8] = "ten";
ranks[9] = "Jack";
ranks[10] = "Queen";
ranks[11] = "King";
ranks[12] = "Ace";
}
public String[] suits = new String[maxSuit - 1];
{
suits[0] = "Clubs";
suits[1] = "Diamonds";
suits[2] = "Spades";
suits[3] = "Hearts";
}
public String suit = suits[nSuit]; //The suit string of the card whose suit number is nSuit
public String rank = ranks[nRank]; //Same but with ranks
//Constructor for the Card object, with two arguments, x for rank, y for suit
public Card(int x,int y){
this.nRank = x;
this.nSuit = y;
}
//method to get which card it is in a string
public String whatCard(){
return rank + " of " + suit;
}
}
套牌:
public class Deck {
public static int nRanks = 13; //number of ranks
public static int nSuits = 4; // number of suits
public static int nCard = nRanks * nSuits; // number of cards
Card[] deck = new Card[nCard -1]; //new array called deck to store all the cards
int h = 0; //a variable to control the place of each card in the array
//constructor for the Deck
public Deck() {
while(h < 52){ // loop until there are 52 cards
// cycles through all the possible combinations between i(ranks) and j(suits) and creates a card with each
for(int i = 1; i <= nRanks; i++){
for(int j = 1; j <= nSuits; j++){
deck[h] = new Card(i,j); // creation of the card
h++; // adds 1 to to h so the program knows how many cards are there
}
}
}
}
//method for getting a card depending on its position in the array(x)
public Card getCard(int x){
return deck[x-1];
}
}
还有牌/牌组的显示,我称之为随机播放:
public class Shuffle {
public static void main(String[] args){
Deck newDeck = new Deck(); // creates a new Deck object
//loops through all the cards in the deck
for(int i = 0; i < Deck.nCard; i++){
System.out.println(newDeck.getCard(i).whatCard()); // prints each card
}
}
}
虽然 Eclipse 没有注意到代码中的任何错误,但当我尝试编译时,我会看到以下内容:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
at Card.<init>(Card.java:26)
at Deck.<init>(Deck.java:20)
at Shuffle.main(Shuffle.java:5)
我错过了什么?
最佳答案
在所有长度为 N 的循环中,您尝试从元素 1..N 进行访问。您应该从 0..(1-N) 开始循环。
例如,如果您有 4 个花色,则您将拥有一个包含元素 0、1、2、3 的数组。通过说(for j=1; j<=4, j++)
,您正在尝试访问超出范围的元素 4。
它应该是 (j=0; j<4; j++)
.
关于java - 一副牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10858674/
我正在尝试模拟“发现它!”的套牌用 Python 制作的卡片。对于那些不知道什么是“Spot it!”的人is- 这是一种流行的纸牌游戏,一副牌有 55 张牌,每张牌上有 8 个随机符号(例如球、波浪
我目前正在尝试解决与其他人编写的程序相关的问题,该程序使用 Jackcess 版本 1.1.8 将信息写入 Access 数据库。在向给定 Access 表添加 400 万行的运行中,出现以下异常:
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我是一名优秀的程序员,十分优秀!