我正在自学Java的过程中,我正尝试使用我迄今为止学到的不同内容创建一个卡洗牌应用程序。 我知道有一种更简单的方法可以在一个班级内完成此任务,但其目标是将迄今为止学到的很多内容实施到一个程序中。这里的问题是,当我将每个套装组合到组合数组中时,组合数组的索引读取“null”。我知道这个问题在Randomize
类中。Java Card Shuffling App
createCards类别:
public class createCards {
decoyObject d = new decoyObject();
public void storeHearts(){
String[] heartRay = new String[13];
heartRay[0] = "AceH";
int L = heartRay.length - 4;
for(int i = 0; i <= L; i++){
Integer h = i + 2;
String heartPlace = h.toString()+"H";
heartRay[i+1] = heartPlace;
}
heartRay[10] = "JackH";
heartRay[11] = "QueenH";
heartRay[12] = "KingH";
d.setHearts(heartRay);
}
public void storeClubs(){
String[] clubRay = new String[13];
clubRay[0] = "AceC";
int L = clubRay.length - 4;
for(int i = 0; i <= L; i++){
Integer h = i + 2;
String clubPlace = h.toString() + "C";
clubRay[i+1] = clubPlace;
}
clubRay[10] = "JackC";
clubRay[11] = "QueenC";
clubRay[12] = "KingC";
d.setClubs(clubRay);
}
public void storeSpades(){
String[] spadeRay = new String[13];
spadeRay[0] = "AceS";
int L = spadeRay.length - 4;
for(int i = 0; i <= L; i++){
Integer h = i + 2;
String spadePlace = h.toString() + "S";
spadeRay[i+1] = spadePlace;
}
spadeRay[10] = "JackS";
spadeRay[11] = "QueenS";
spadeRay[12] = "KingS";
d.setSpades(spadeRay);
}
public void storeDiamonds(){
String[] diamondRay = new String[13];
diamondRay[0] = "AceD";
int L = diamondRay.length - 4;
for(int i = 0; i <= L; i++){
Integer h = i + 2;
String diamondPlace = h.toString() + "D";
diamondRay[i+1] = diamondPlace;
}
diamondRay[10] = "JackD";
diamondRay[11] = "QueenD";
diamondRay[12] = "KingD";
d.setDiamonds(diamondRay);
}
}
decoyObject类
public class decoyObject {
private String[] clubs;
private String[] hearts;
private String[] spades;
private String[] diamonds;
private String[] cards;
public decoyObject(){
this.clubs = new String[13];
this.hearts = new String[13];
this.spades = new String[13];
this.diamonds = new String[13];
this.cards = new String[52];
}
public void setClubs(String[] clubs){
this.clubs = clubs;
}
public String[] getClubs(){
return clubs;
}
public void setHearts(String[] hearts){
this.hearts = hearts;
}
public String[] getHearts(){
return hearts;
}
public void setSpades(String[] spades){
this.spades = spades;
}
public String[] getSpades(){
return spades;
}
public void setDiamonds(String[] diamonds){
this.diamonds = diamonds;
}
public String[] getDiamonds(){
return diamonds;
}
public void setCards(String[] cards){
this.cards = cards;
}
public String[] getCards(){
return cards;
}
}
随机化类
我相信这个类是在问题发生
public class Randomize{
createCards c = new createCards();
decoyObject d = new decoyObject();
public void randomizeCards(){
c.storeHearts();
c.storeClubs();
c.storeDiamonds();
c.storeSpades();
//I believe the issue happens in the code below
String[] randomHearts = d.getHearts();
String[] randomClubs = d.getClubs();
String[] randomDiamonds = d.getDiamonds();
String[] randomSpades = d.getSpades();
/***************************************/
String[] combinedCards = new String[52];
for (int i = 0; i <13; i++){
combinedCards[i] = randomHearts[i];
}
for (int i = 0; i <13; i++){
combinedCards[i+13] = randomClubs[i];
}
for (int i = 0; i <13; i++){
combinedCards[i+26] = randomDiamonds[i];
}
for (int i = 0; i <13; i++){
combinedCards[i+39] = randomSpades[i];
}
//THE CODE BELOW PRINTS OUT NULL 52 TIMES
for (String cards : combinedCards){
System.out.println(cards);
}
/**********************************/
}
}
Funthings类
这是主要的方法的类。
public class Funthings {
public static void main(String[] args) {
Randomize r = new Randomize();
r.randomizeCards();
}
}
你的诱饵对象从未初始化卡的西装,他们都是空。 – 2014-10-27 04:31:04
我以为我在decoyObject类的构造函数中初始化它们!? – 2014-10-27 04:35:54
你正在用数组的全部空字符串初始化它们。 – CandiedOrange 2014-10-27 04:36:43