0

我刚开始Java,我不明白为什么这段代码:

char WORDS[][] = {"jazz", "buzz", "hajj", "fuzz", "jinx", "jazzy", "fuzzy", "faffs", "fizzy", "jiffs", "jazzed", "buzzed", "jazzes", "faffed", "fizzed", "jazzing", "buzzing", "jazzier", "faffing", "fuzzing"};
char comp[] = WORDS[rand.nextInt(WORDS.length)];
char compHidden[comp.length];

产生此错误:

- Syntax error on token "[", = expected
- Type mismatch: cannot convert from int 
 to char
- Syntax error on token "]", delete this 
 token

都在第三行。为什么是这样?还有另一种方法可以找到 char 数组的长度吗?

4

2 回答 2

0

这里需要一个分配,例如:

char compHidden[] = new char[comp.length];
于 2013-05-06T08:42:53.453 回答
0

WORDS数组没有被实例化为多维数组,而是被实例化为常规数组,char[]并且应该这样声明。数组的类型也不应该是char[],而是应该String[]给出分配的数组类型。

改变:

char WORDS[][] = {"jazz", "buzz", "hajj", "fuzz", "jinx", "jazzy", "fuzzy", "faffs", "fizzy", "jiffs", "jazzed", "buzzed", "jazzes", "faffed", "fizzed", "jazzing", "buzzing", "jazzier", "faffing", "fuzzing"};

String WORDS[] = {"jazz", "buzz", "hajj", "fuzz", "jinx", "jazzy", "fuzzy", "faffs", "fizzy", "jiffs", "jazzed", "buzzed", "jazzes", "faffed", "fizzed", "jazzing", "buzzing", "jazzier", "faffing", "fuzzing"};
于 2013-05-06T08:44:40.983 回答