我正在参加Java入门课程的过渡课程,我们有一个处理project子手游戏的项目。我已经完成了大部分代码,但是有一个错误和一些我似乎无法解决的问题。
首先,程序显示星号形式的伪装词,然后提示用户输入字母,然后程序显示用户是否做出正确/不正确的猜测,并带有猜测的数量和错误的猜测数量,伪装单词中的字母替换为所述字母。
出于测试目的,单词use是html
。
因此,如果我猜到字母,该程序应输出:
期望
The disguised word is ****
Guess a letter: h
Correct. The number of guess made is 1 with 0 wrong
实际
The disguised word is ****
Guess a letter: h
Incorrect. The number of guess made is 1 with 1 wrong
The disguised word is h***
Guess a letter: m
Incorrect. The number of guess made is 2 with 2 wrong
The disguised word is h*m*
Guess a letter: t
Correct. The number of guess made is 3 with 3 wrong
Good job, you found the secret word which is html
我遇到的问题是,当我猜到正确的字母时,程序将返回“不正确”(假定正确)。此外,错误的计数也无法正常工作。
由于尚未被教导,我们不允许使用数组或字符串生成器,并且保留供项目2使用。
这是我的代码:
Hangman.java
public class Hangman {
private String secretWord;
private String disguisedWord = "";
private String result;
private int guesses = 0;
private int wrongGuess = 0;
public void setSecretWord(String word) {
secretWord = word;
}
public void setDisguisedWord() {
for (int i = 0; i < secretWord.length(); i++) {
disguisedWord += "*";
}
}
public void guesses() {
guesses++;
}
public boolean guessLetter(char c) {
for (int i = 0; i < disguisedWord.length(); i++) {
if (c == secretWord.charAt(i)) {
disguisedWord = disguisedWord.substring(0, i) + c + disguisedWord.substring(i + 1);
result = "Correct.";
} else {
result = "Incorrect.";
}
}
if (secretWord.equals(disguisedWord)) {
return true;
} else {
wrongGuess++;
return false;
}
}
public String getDisguisedWord() {
return disguisedWord;
}
public String getResult() {
return result;
}
public int getGuesses() {
return guesses;
}
public int getWrongGuess() {
return wrongGuess;
}
public String getSecretWord() {
return secretWord;
}
}
Game.java
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
String word1 = "html";
String word2 = "css";
String word3 = "java";
Scanner kb = new Scanner(System.in);
Hangman game = new Hangman();
game.setSecretWord(word1);
game.setDisguisedWord();
System.out.println("Let's play a round of hangman.");
System.out.println("We are playing hangman");
while (true) {
System.out.println("");
System.out.println("The disguised word is " + game.getDisguisedWord());
System.out.println("Guess a letter:");
char guess = kb.next().charAt(0);
game.guessCount();
boolean isFound = game.guessLetter(guess);
if (isFound) {
System.out.println(game.getResult() + " No. of guesses made is " + game.getGuesses() + " with " + game.getWrongGuess() + " wrong");
System.out.println("Good job, you found the secret word which is " + game.getSecretWord());
break;
}
else {
System.out.println(game.getResult() + " No. of guesses made is " + game.getGuesses() + " with " + game.getWrongGuess() + " wrong");
}
}
}
}
答案 0 :(得分:1)
问题出在您的guessLetter()
方法中。第一个问题是您每次检查字母时都会覆盖result
,因此,如果该字母与第一个字符匹配但第二个字符不匹配,则返回false。第二个问题是您是否根据单词现在是否已完成而不是字母是否匹配来返回。试试这个:
public boolean guessLetter(char c) {
boolean foundLetter = false;
for (int i = 0; i < disguisedWord.length(); i++) {
if (c == secretWord.charAt(i)) {
disguisedWord = disguisedWord.substring(0, i) + c + disguisedWord.substring(i + 1);
foundLetter = true;
}
}
if (foundLetter) {
result = "Correct.";
return true;
} else {
result = "Incorrect.";
wrongGuess++;
return false;
}
}
编辑: 还要将Game.java更改为
while (true) {
System.out.println("");
System.out.println("The disguised word is " + game.getDisguisedWord());
System.out.println("Guess a letter:");
char guess = kb.next().charAt(0);
game.guesses();
boolean isFound = game.guessLetter(guess);
if (isFound) {
System.out.println(game.getResult() + " No. of guesses made is " + game.getGuesses() + " with " + game.getWrongGuess() + " wrong");
}
else {
System.out.println(game.getResult() + " No. of guesses made is " + game.getGuesses() + " with " + game.getWrongGuess() + " wrong");
}
if (game.getSecretWord().equals(game.getDisguisedWord())) {
System.out.println("Good job, you found the secret word which is " + game.getSecretWord());
break;
}
}
答案 1 :(得分:0)
问题,找到正确的计数,打破循环:
import java.util.Scanner;
public Hangman {
private String secretWord;
private String disguisedWord = "";
private String result;
private int guesses = 0;
private int wrongGuess = 0;
public void setSecretWord(String word) {
secretWord = word;
}
public void setDisguisedWord() {
for (int i = 0; i < secretWord.length(); i++) {
disguisedWord += "*";
}
}
public boolean guessLetter(char c) {
for (int i = 0; i < disguisedWord.length(); i++) {
if (c == secretWord.charAt(i) && disguisedWord.charAt(i) == '*') {
disguisedWord = disguisedWord.substring(0, i) + c + disguisedWord.substring(i + 1);
result = "Correct.";
guesses++;
return true;
}
}
result = "Incorrect.";
wrongGuess++;
return false;
}
public String getDisguisedWord() {
return disguisedWord;
}
public String getResult() {
return result;
}
public int getGuesses() {
return guesses;
}
public int getWrongGuess() {
return wrongGuess;
}
public String getSecretWord() {
return secretWord;
}
}
public class Game {
public static void main(String[] args) {
String word1 = "html";
String word2 = "css";
String word3 = "java";
Scanner kb = new Scanner(System.in);
Hangman game = new Hangman();
game.setSecretWord(word1);
game.setDisguisedWord();
System.out.println("Let's play a round of hangman.");
System.out.println("We are playing hangman");
while (true) {
System.out.println("");
System.out.println("The disguised word is " + game.getDisguisedWord());
System.out.println("Guess a letter:");
char guess = kb.next().charAt(0);
boolean isFound = game.guessLetter(guess);
if (isFound) {
System.out.println(game.getResult() + " No. of guesses made is " + game.getGuesses() + " with " + game.getWrongGuess() + " wrong");
if (game.getDisguisedWord().equals(game.getSecretWord())) {
System.out.println("Good job, you found the secret word which is " + game.getSecretWord());
break;
}
}
else {
System.out.println(game.getResult() + " No. of guesses made is " + game.getGuesses() + " with " + game.getWrongGuess() + " wrong");
}
}
}
}