gpt4 book ai didi

Java 洗牌应用程序

转载 作者:行者123 更新时间:2023-12-01 12:22:23 25 4
gpt4 key购买 nike

我正在自学 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 类

这是具有 main 方法的类。

public class Funthings {


public static void main(String[] args) {

Randomize r = new Randomize();
r.randomizeCards();


}
}

最佳答案

从 createCards 返回相同的 decoyObject 到 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);
}
/**********************************/

}

}

关于Java 洗牌应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26581169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com