Java - Hangman 游戏 - StringBuffer 变量上的 charAt 有问题
Java - Hangman Game - trouble with charAt on StringBuffer variable
所以我正在尝试使用 returns 随机词的网站制作一个绞刑架游戏。我在刽子手游戏中使用了那个随机词。
我坚持的是验证用户的猜测。这是代码,我只是先将所有内容放在 main 中,然后再制作单独的方法来为我完成工作。
public static void main(String[] args) throws Exception {
randomWord = TestingStuff.sendGet();
int totalTries = 1;
char[] guesses = new char[26];
int length = randomWord.length();
Scanner console = new Scanner(System.in);
System.out.print("* * * * * * * * * * * * * * *"
+ "\n* Welcome to Hangman! *"
+ "\n* * * * * * * * * * * * * * *");
System.out.println("\nYou get 10 tries to guess the word by entering in letters!\n");
System.out.println(randomWord);
/*
Cycles through the array based on tries to find letter
*/
while (totalTries <= 10) {
System.out.print("Try #" + totalTries);
System.out.print("\nWhat is your guess? ");
String guess = console.next();
char finalGuess = guess.charAt(0);
guesses[totalTries - 1] = finalGuess; //Puts finalGuess into the array
for (int i = 0; i < totalTries; i++) { //checks to see if the letter is already guessed
if (finalGuess != guesses[i]) {
for (int j = 0; i < length; j++) { //scans each letter of random word
if (finalGuess.equals(randomWord.charAt(j))) {
}
}
} else {
System.out.println("Letter already guessed, try again! ");
}
}
}
}
我坚持的是 while 循环内部,它说:
for (int i = 0; i < totalTries; i++) { //checks to see if the letter is already guessed
if (finalGuess != guesses[i]) {
for (int j = 0; i < length; j++) { //scans each letter of random word
if (finalGuess.equals(randomWord.charAt(j))) {
}
}
} else {
System.out.println("Letter already guessed, try again! ");
}
}
它给我一个错误 "char cannot be dereferenced"。我在这里错过了什么吗?
finalGuess
是原始的 char
- 您不能在其上使用诸如 equals
之类的方法。您可以使用 ==
运算符比较两个 char
:
if (finalGuess == randomWord.charAt(j)) {
所以我正在尝试使用 returns 随机词的网站制作一个绞刑架游戏。我在刽子手游戏中使用了那个随机词。
我坚持的是验证用户的猜测。这是代码,我只是先将所有内容放在 main 中,然后再制作单独的方法来为我完成工作。
public static void main(String[] args) throws Exception {
randomWord = TestingStuff.sendGet();
int totalTries = 1;
char[] guesses = new char[26];
int length = randomWord.length();
Scanner console = new Scanner(System.in);
System.out.print("* * * * * * * * * * * * * * *"
+ "\n* Welcome to Hangman! *"
+ "\n* * * * * * * * * * * * * * *");
System.out.println("\nYou get 10 tries to guess the word by entering in letters!\n");
System.out.println(randomWord);
/*
Cycles through the array based on tries to find letter
*/
while (totalTries <= 10) {
System.out.print("Try #" + totalTries);
System.out.print("\nWhat is your guess? ");
String guess = console.next();
char finalGuess = guess.charAt(0);
guesses[totalTries - 1] = finalGuess; //Puts finalGuess into the array
for (int i = 0; i < totalTries; i++) { //checks to see if the letter is already guessed
if (finalGuess != guesses[i]) {
for (int j = 0; i < length; j++) { //scans each letter of random word
if (finalGuess.equals(randomWord.charAt(j))) {
}
}
} else {
System.out.println("Letter already guessed, try again! ");
}
}
}
}
我坚持的是 while 循环内部,它说:
for (int i = 0; i < totalTries; i++) { //checks to see if the letter is already guessed
if (finalGuess != guesses[i]) {
for (int j = 0; i < length; j++) { //scans each letter of random word
if (finalGuess.equals(randomWord.charAt(j))) {
}
}
} else {
System.out.println("Letter already guessed, try again! ");
}
}
它给我一个错误 "char cannot be dereferenced"。我在这里错过了什么吗?
finalGuess
是原始的 char
- 您不能在其上使用诸如 equals
之类的方法。您可以使用 ==
运算符比较两个 char
:
if (finalGuess == randomWord.charAt(j)) {