2014-10-27 49 views
0

我正在自学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(); 


} 
} 
+0

你的诱饵对象从未初始化卡的西装,他们都是空。 – 2014-10-27 04:31:04

+0

我以为我在decoyObject类的构造函数中初始化它们!? – 2014-10-27 04:35:54

+0

你正在用数组的全部空字符串初始化它们。 – CandiedOrange 2014-10-27 04:36:43

回答

1

将相同的decoyObject从createCards返回到Randomize应解决该问题。

变化:增加)称为storeCards(一种新的方法createCards类将返回decoyObject的方法调用中randomizeCards()

createCards.java

public class createCards { 

decoyObject d = new decoyObject(); 

public decoyObject storeCards(){ 
    storeHearts(); 
    storeClubs(); 
    storeSpades(); 
    storeDiamonds(); 
    return d; 
} 

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); 

} 

} 

Randomize.java

public class Randomize{ 

createCards c = new createCards(); 

public void randomizeCards(){ 
    decoyObject d = null; 
    d = c.storeCards(); 

    //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); 
} 
/**********************************/ 

} 

} 
+0

工作完美。谢谢! – 2014-10-31 02:30:21

+0

@ user3386635不客气! – 2014-10-31 05:54:05

0

当在随机化调用d.getHearts(),d是具有decoyObject类的一个对象和你要处理getHearts()该对象的,但是要分配的值在以阵列将createCards类添加到decoyObject类的全新对象中。

您没有使用相同的对象,而是使用两个不同的对象将值分配到测定中并从数组中读取值。这就是为什么你得到空值。

祝你好运!!!

+0

我想我明白你的意思了。所以当我使用c.storeHearts时,即使c.storeHearts使用了decoyObject的“set”方法。 Randomize类中的decoyObject将为空,因为每个实例化都会创建一个唯一的类。是对的吗? – 2014-10-27 04:45:43

+0

您使用相同的类,但创建了两个不同的对象。当你使用新的关键字时,它会在内存堆中创建新的对象。我认为最好是在Randomize中创建一个decoyObject对象,并将其作为createCards的构造函数的参数传递。那么你必须初始化decoyObject的数组值。取回该对象并存储在Randomize中声明的引用变量中。然后,您可以使用该对象将值添加到Randomize对象中的combinedCards数组中。试着让我知道.... – 2014-10-27 04:54:29