[英]Use package-private class in the public class
我想创建一个称为对的简单纸牌游戏。 基本上,一个玩家一次掷出2张牌,如果有2张牌匹配,它们将保持正面朝上。 如果不匹配,则将它们反转回到正面朝下的位置。 我创建了一个包私有类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 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
}
}
您正在从Card类访问值,但从未声明该变量。像这样更改Card类,它将起作用
class Card{
boolean flipped; // check if card is flipped
char value;
Card(char value){
value = value;
}
}
您的错误来自您错过了Card类中的value
字段的事实。
class Card{
boolean flipped; // check if card is flipped
char value;
Card(char value){
this.value = value;
}
}
卡类没有任何值名称的数据成员。
这是您的卡类代码
class Card{
boolean flipped; // check if card is flipped
Card(char value){
value = value;
}
}
因此,您需要在Card Class中创建值作为数据成员
样例代码
class Card{
boolean flipped; // check if card is flipped
char value; //here you need to create value data member
//datatype you can change according to your requirement
Card(char value){
this.value = value; //here need to write this.value because you want assign value to class member
}
}
希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.