我正在教自己Java和我正在尝试使用我迄今学到的不同东西创建一个洗牌应用程序。 我知道在一个课程中有一种更简单的方法可以做到这一点,但我们的目标是实现迄今为止我学到的很多东西到一个程序中。这里的问题是当我将每个套装组合成一个在组合数组中,组合数组的索引读取" null"。我知道这个问题属于Randomize
类。
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 Class
这是主要方法的类。
public class Funthings {
public static void main(String[] args) {
Randomize r = new Randomize();
r.randomizeCards();
}
}
答案 0 :(得分:1)
将相同的decoyObject从createCards返回到Randomize应该可以解决问题。
更改:在 createCards 类中添加了一个名为 storeCards()的新方法,该方法会将 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);
}
/**********************************/
}
}
答案 1 :(得分:0)
当你在Randomize中调用d.getHearts()时,d有一个decoyObject类的对象,你将要处理该对象的getHearts(),但是你要在createCards类的数组中赋值给decoyObject类的全新对象。
您没有使用相同的对象,并且您正在使用两个不同的对象来分配分析到分析并从数组中读取值。这就是你得到空值的原因。
祝你好运!!!