[英]ArrayList Novice Error in Java
I am trying to get more proficient with Java. 我正在尝试精通Java。 I am making a simple card game to test my basic skills.
我正在制作一个简单的纸牌游戏来测试我的基本技能。 I have come across a problem in my Deck class.
我在Deck班上遇到了一个问题。 The for-loops are not creating new card objects in my arraylist even though they should.
for循环即使应该也不会在我的arraylist中创建新的卡对象。 When I call getTotalCards the size of the arraylist is always 0. Any ideas of what I am doing wrong?
当我调用getTotalCards时,arraylist的大小始终为0。对我做错的任何想法吗?
Main Class: 主类:
package gameofcards;
import java.util.Random;
import static gameofcards.Card.*;
public class GameOfCards {
public static void main(String[] args) {
Deck d1 = new Deck();
System.out.println(d1.getTotalCards());
}
}
Deck Class: 甲板等级:
package gameofcards;
import java.util.ArrayList;
public class Deck {
private ArrayList<Card> cards;
public Deck(){
cards = new ArrayList<Card>();
for(int i = 1; i >=4; i++){
for(int j = 1; j >=13; j++){
cards.add(new Card(i,j));
}
}
}
public int getTotalCards(){
return cards.size();
}
} }
Card Class: 卡类:
package gameofcards;
public class Card {
private int Suite;
private int Rank;
public static final int Club = 1;
public static final int Diamond = 2;
public static final int Hearts = 3;
public static final int Spade = 4;
public static final int Jack = 10;
public static final int Queen = 11;
public static final int King = 12;
public static final int Ace = 13;
public void setSuite(int cardSuite){
Suite=cardSuite;
}
public int getSuite(){
return Suite;
}
public void setRank(int cardRank){
Rank=cardRank;
}
public int getRank(){
return Rank;
}
public Card(int Suite, int Rank){
this.Suite = Suite;
this.Rank = Rank;
}
public String cardSuite(){
switch(Suite){
case Club: return "Clubs";
case Diamond: return "Diamonds";
case Hearts: return "Hearts";
case Spade: return "Spades";
default: return "Joker";
}
}
public String cardRank() {
switch(Rank){
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "Jack";
case 11: return "Queen";
case 12: return "King";
case 13: return "Ace";
default: return "Joker";
}
}
}
The conditions in for loop are wrong. for循环中的条件是错误的。 should be i<=4 and j<=13
应该是i <= 4和j <= 13
for(int i = 1; i <=4; i++){
for(int j = 1; j <=13; j++){
cards.add(new Card(i,j));
}
}
Your loop conditions are wrong. 您的循环条件是错误的。
Instead of: 代替:
for(int i = 1; i >=4; i++){
It should be: 它应该是:
for(int i = 1; i <=4; i++){
And a similar problem with the inner loop condition. 内循环条件也有类似的问题。
You want to loop while i
is less than or equal to 4. 您想在
i
小于或等于4时循环播放。
It wasn't adding cards because it never entered the loop in the first place. 它之所以没有添加卡片,是因为它从来没有进入循环。
An easy way to have figured this out would have been to put a println
inside the loop and see what happens. 解决这一问题的一种简单方法是将一个
println
放入循环中,然后看看会发生什么。 You would have noticed that it never printed. 您会注意到它从未打印过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.