我打算一個卡牌遊戲
有兩種卡,分別是ValueCard和FunctionCard,它們都有數字
FunctionCard還多了加減乘的功能,我先用String把三種功能分數
ValueCard類和FunctionCard類都是extends Card類的,因為它們都有數字
然後我在Deck 類裡面 放了ValueCard和FunctionCard的物件
當生成Deck 物件就會生成40張ValueCard和48張FunctionCard個Deck 裡面的變量
class Card {
private int value;
public Card(int value) {
this.value = value;
}
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public String toString() {
return "[" + getValue() + "]";
}
}
class ValueCard extends Card {
public ValueCard(int value) {
super(value);
}
@Override
public void setValue(int value) {
super.setValue(value);
}
@Override
public int getValue() {
return super.getValue();
}
@Override
public String toString() {
return super.toString();
}
}
class FunctionCard extends Card {
private String function;
public FunctionCard(int value, String function) {
super(value);
setFunction(function);
}
@Override
public void setValue(int value) {
super.setValue(value);
}
@Override
public int getValue() {
return super.getValue();
}
public void setFunction(String function) {
this.function = function;
}
public String getFunction() {
return function;
}
}
class Deck {
private Card[] vCard = new ValueCard[40];
private Card[] fCard = new FunctionCard[48];
public Deck() {
int k = 0;
while (k < vCards.length)// Generate 1-9, repeat 4 times, a total of 40 Value Cards.
for (int i = 0; i < 9; i++) {
vCard[k++] = new ValueCard(i);
}
k = 0;
int num[] = { -1, 0, 2 };
while (k < 19)// Generate 1-9, repeat 2 times, a total of 18 Add Cards.
for (int i = 1; i <= 9; i++) {
fCard[k++] = new FunctionCard(i, "+");
}
while (k > 18 && k < 36)// Generate 1-9, repeat 2 times, a total of 18 Deduct Cards.
for (int i = 1; i <= 9; i++) {
fCard[k++] = new FunctionCard(i, "-");
}
while (k > 35 && k < fCards.length)// Generate -1, 0, 2 , repeat 4 times, a total of 12 Multiply Cards.
for (int i = 0; i <= 2; i++) {
fCard[k++] = new FunctionCard(num[i], "*");
}
}
}
class Player {
private Card[] vCard = new ValueCard[5];
private Card[] fCard = new FunctionCard[5];
}
class Game {
public static void main(String[] args) {
Deck d = new Deck();
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
vCards cannot be resolved to a variable
fCards cannot be resolved to a variable
at Deck.<init>(Game.java:77)
at Game.main(Game.java:101)
我試過用全局變量,和Deck 類的變量改成pubile都是報相同的錯誤
當生成Deck物件就會產生ValueCard和FunctionCard的變量,並可以放入Player類的變量裡
你应该是写错变量名了 ,你定义的是 vCard,你这里却用的 vCards
接上面大佬回答的,检查你的while循环的条件,下面还有用了一个fCards,理论上来说idea的智能性应该在编写是不会出现这种失误