[英]Java Card Shuffling App
我正在自學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);
}
/**********************************/
}
}
趣味課
這是具有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);
}
/**********************************/
}
}
當您在Randomize上調用d.getHearts()時,d有一個decoyObject類的對象,並且您將處理該對象的getHearts(),但是您要為createCards類的數組賦值給全新的decoyObject類的對象。
您使用的不是同一對象,而是使用兩個不同的對象將值分配到化驗中並從數組中讀取值。 這就是為什么您得到空值的原因。
祝好運 !!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.