英文:
Troubled with how and where to implement if/else statements
问题
我几乎已经完成了一个用Java创建的Hangman游戏,尽管我在最后一个部分遇到了困难。我想让程序检查单词的所有字母是否都被正确猜测,如果是,游戏会打印一条消息,表示他们赢了,游戏结束,并且程序进入主类中的do while循环,询问是否想再玩一次。然而,如果没有猜测正确,游戏会继续进行,直到达到这一点,或者如果已经用完了5次猜测 - 这时,程序会返回主循环,重新开始游戏,而不是终止程序。
问题是我不确定如何在哪里以及如何构建if和else语句,以实现上述目标。
Instantiable Class
public class Hangman {
// ... (你的代码,不做翻译)
public String getEndMessage() {
return endMessage;
}
}
Main Class
import javax.swing.*;
public class HangmanApp {
public static void main(String[] args) {
// ... (你的代码,不做翻译)
do {
Hangman myHangman = new Hangman();
// ... (你的代码,不做翻译)
for (int i = 0; i < 10; i++) {
// ... (你的代码,不做翻译)
if (outputWord.equals(hiddenWord)) {
JOptionPane.showMessageDialog(null, "Congratulations! You've won!");
break;
}
// ... (你的代码,不做翻译)
}
numberLives = myHangman.getNumberLives();
JOptionPane.showMessageDialog(null, "You have finished the game with: " + numberLives + " lives!");
restartGame = JOptionPane.showInputDialog(null, "Would you like to play again?");
} while (restartGame.equalsIgnoreCase("Yes"));
}
}
以上是你的代码的翻译部分,仅包含注释的翻译。如果你有任何问题或需要进一步帮助,请随时问我。
英文:
I am almost finished creating a hangman game in Java, although I am having difficulty with one last part. I want to make it so the program checks if all the letters of the word have been guessed correctly and if so, the game prints a message saying they have won, the game ends and the program goes to the do while loop in the main class asking if they would like to play again. If not however, the game continues until this point or if all 5 guesses have been used - to which again, it is sent to the do while loop in order to restart the game and not simply terminate the program.
The problem is I am unsure how and where exactly to structure the if and else statements in order to do so.
Any help would be greatly appreciated, if I can provide further information to help narrow anything down please ask too, thank you in advance.
Instantiable Class
public class Hangman {
private char letterGuess;
private int numberLives;
private String outputWord, endMessage;
private final String hiddenWord;
private final StringBuffer swapBuffer = new StringBuffer();
public Hangman() {
letterGuess = ' ';
numberLives = 5;
hiddenWord = "java";
outputWord = "";
endMessage = "";
for (int i = 0; i < hiddenWord.length(); i++) {
swapBuffer.append("*");
}
}
public void setLetterGuess(char letterGuess) {
this.letterGuess = letterGuess;
}
public void compute() {
boolean letterFound = false;
for (int i = 0; i < hiddenWord.length(); i++) {
if (letterGuess == hiddenWord.charAt(i)) {
swapBuffer.setCharAt(i, letterGuess);
letterFound = true;
}
}
if (!letterFound) numberLives--;
outputWord = swapBuffer.toString();
}
public int getNumberLives() {
return numberLives;
}
public String getHiddenWord() {
return hiddenWord;
}
public String getOutputWord() {
return outputWord;
}
public String getEndMessage() {
return endMessage;
}
}
Main Class
import javax.swing.*;
public class HangmanApp {
public static void main(String[] args) {
char letterGuess;
int numberLives;
String hiddenWord, outputWord, endMessage, restartGame;
do {
Hangman myHangman = new Hangman();
JOptionPane.showMessageDialog(null, "Welcome to Java Hangman!");
JOptionPane.showMessageDialog(null, "In this game, a word will be printed to you in asterisks - each letter will be revealed upon a correct guess!");
JOptionPane.showMessageDialog(null, "You have 5 lives for the game, the game will end if you make too many incorrect guesses!");
for (int i = 0; i < 10; i++) {
hiddenWord = myHangman.getHiddenWord();
numberLives = myHangman.getNumberLives();
JOptionPane.showMessageDialog(null, "You currently have " +numberLives+ " lives!");
letterGuess = JOptionPane.showInputDialog(null, "Now, please enter a letter : ").charAt(0);
myHangman.setLetterGuess(letterGuess);
myHangman.compute();
outputWord = myHangman.getOutputWord();
JOptionPane.showMessageDialog(null, "The word so far is : " +outputWord);
}
numberLives = myHangman.getNumberLives();
JOptionPane.showMessageDialog(null, "You have finished the game with : " +numberLives+ " lives!");
restartGame = JOptionPane.showInputDialog(null, "Would you like to play again?");
}
while (restartGame.equalsIgnoreCase("Yes"));
}
}
答案1
得分: 1
这是我看到的方式。在调用 myHangman.compute();
之后,您想要检查玩家是否赢得了游戏或者已经用尽了生命。这两种情况都可以使用一个 if
表达式进行单独的测试。没有 else
部分,因为如果测试失败,游戏会继续进行。
如果识别出其中一种情况,那么您希要显示一条消息,然后退出当前的游戏循环,以便询问用户是否想要玩另一局游戏。实现这个的方法是使用 break
语句。
您遇到的一个问题是,在主游戏循环中没有办法询问 Hangman
类是否用户已经猜对了整个单词。为了能够做到这一点,您可以将以下方法添加到 Hangman
类的末尾:
public boolean getAllLettersFound() {
return outputWord.indexOf('*') < 0;
}
该方法检查 outputWord
中是否存在任何 *
。如果没有,那么用户已经猜对了所有字母。因此,通过添加这个方法,主循环可以查询 Hangman
对象,以确定玩家是否赢得了游戏。
将所有内容放在一起,您需要在主游戏循环中的 myHangman.compute();
调用之后,添加这两个条件检查。以下是这两个 if
块的内容:
if (myHangman.getAllLettersFound()) {
JOptionPane.showMessageDialog(null, "You Won!!!!");
break;
}
if (myHangman.getNumberLives() == 0) {
JOptionPane.showMessageDialog(null, "You Ran Out Of Guesses");
break;
}
就是这样。祝您编程愉快!
英文:
Here's the way I see it. After you call myHangman.compute();
, you want to check to see if the player has either won the game or run out of lives. Each of these is a separate test using an if
expression. There is no else
part because if the test fails, the game just goes on.
If one of these conditions is recognized, then you want to display a message, and then you want to exit the current game loop so the user will be asked if they want to play another game. The way to do this is with the break
statement.
One issue you have is that you don't have a way in your main game loop to ask the Hangman
class if the user has guessed the whole word. To be able to do this, you can add this method to the bottom of your Hangman
class:
public boolean getAllLettersFound() {
return outputWord.indexOf('*') < 0;
}
This method checks to see if there are any *
in outputWord
. If not, then the user has guessed all the letters. So with this method added, the main loop can query the Hangman
object to find out if the player has won.
To put this all together, you need to add the two condition checks to your main game loop, right after you call myHangman.compute();
. Here are those two if
blocks:
if (myHangman.getAllLettersFound()) {
JOptionPane.showMessageDialog(null, "You Won!!!!");
break;
}
if (myHangman.getNumberLives() == 0) {
JOptionPane.showMessageDialog(null, "You Ran Out Of Guesses");
break;
}
That should do it. Happy coding!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论