gpt4 book ai didi

java - 在公共(public)类中使用包私有(private)类

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

我想创建一个名为 Pairs 的简单纸牌游戏。基本上,玩家翻 2 张牌,一次一张,如果 2 张牌匹配,则它们保持面朝上;如果不匹配,它们将被反转回面朝下的位置。我创建了一个包私有(private)类 Card 来存储卡片的实际值(字符)以及它是否翻转(面朝上)。但我收到了错误

MatchCardGame.java:57: error: cannot find symbol
showBoard[i-1] = tempCard2.value + "(" + i + ") ";
^
symbol: variable value
location: variable tempCard2 of type Card
MatchCardGame.java:61: error: cannot find symbol
showBoard[i-1] = tempCard1.value + "(" + i + ")";
^
symbol: variable value
location: variable tempCard1 of type Card
2 errors

当我尝试运行以下代码时...

public class MatchCardGame {

public char[] gameCards;
int gcCount, showCount;
String[] showBoard;
char firstCard = 'a';
char cardValue;
Card tempCard1, tempCard2;
int flipCount = 0;

public MatchCardGame(int n){ // n is the size of the game set by the player in the main, it could only be a multiply of four

// Check if input n is valid
if ((n % 4) != 0 || n < 4 || n > 104) System.exit(0);

// Create an array of cards used in the game
// here we're using a-z as cards thus min = 4 and max = 26*4
gcCount = 0;
gameCards = new char[n];
for (int i = 0;i < (n / 4); i++){
for (int j = 0; j < 4; j++){
gameCards[gcCount] = firstCard;
gcCount++;
}
firstCard++;
}

// Display the back of the cards array
showCount = 1;
showBoard = new String[n];
for (int i = 0; i < n; i++){
showBoard[showCount-1] = "X(" + showCount + ") ";
showCount++;
}

// Create an array of object card, assign each card with a corresponding value in the gameCards array
Card[] cardArray = new Card[n];
for (int i = 0; i < cardArray.length; i++){
cardArray[i] = new Card(gameCards[i]);
}

}

// String representation of cards array
public String boardToString(){
StringBuilder builder = new StringBuilder();
for(String board : showBoard){
builder.append(board);
}
return builder.toString();
}

// flip the card - if already faced up or picked an invalid card, don't flip
public boolean flip (int i){
if (flipCount % 2 == 0){
tempCard2 = new Card(gameCards[i-1]);
showBoard[i-1] = tempCard2.value + "(" + i + ") ";
tempCard2.flipped = true;
}else{
tempCard1 = new Card(gameCards[i-1]);
showBoard[i-1] = tempCard1.value + "(" + i + ")";
tempCard1.flipped = true;
}
flipCount++;
return true;
}

// returns true if card1 and card2 are matched - only executes when an even # of flips are made
// public boolean wasMatch(){}

// if card1 and card2 create a mismatch, reverse them back to faced down position
// public void flipMismatch(){}

// if all cards are flipped and matched, game is over
// public boolean gameOver(){}

// count the # of flips made during the game
// public int getFlips(){}

public static void main(String[] args) {
//set up reader to take inputs
java.util.Scanner reader = new java.util.Scanner (System.in);

int n = 16; //game size

MatchCardGame g1 = new MatchCardGame(n);
System.out.println(g1.boardToString());
g1.flip(5);
System.out.println(g1.boardToString());
}


}

class Card{
boolean flipped; // check if card is flipped
Card(char value){
value = value;
}
}

这样做的正确语法是什么?或者有没有更简单的方法来编写这个游戏? (保留方法...)

最佳答案

您的卡类别不正确:

class Card{
boolean flipped; //This is an attribute
Card(char value){
value = value; // This line does nothing!!!
}
}

我想你的类(class)应该是这样的:

class Card{
boolean flipped; // check if card is flipped
char value;
Card(char val){
this.value = val; // "this" is not necessary, but makes it more readable
}

}

关于java - 在公共(public)类中使用包私有(private)类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41954503/

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