Java 如何以及在何处实现if/else语句
我几乎完成了用Java创建一个刽子手游戏,尽管我在最后一部分遇到了困难。我想让程序检查单词的所有字母是否猜对了,如果猜对了,游戏会打印一条消息说他们赢了,游戏结束,程序进入主类中的do-while循环,询问他们是否愿意再次玩。然而,如果不是这样,游戏将一直持续到这一点,或者如果已经使用了所有5次猜测,那么游戏将再次发送到do while循环,以便重新启动游戏,而不是简单地终止程序 问题是我不确定如何以及在何处构造if和else语句,以便这样做 任何帮助将不胜感激,如果我可以提供进一步的信息,以帮助缩小任何东西,请问太多,提前谢谢你 可实例化类Java 如何以及在何处实现if/else语句,java,Java,我几乎完成了用Java创建一个刽子手游戏,尽管我在最后一部分遇到了困难。我想让程序检查单词的所有字母是否猜对了,如果猜对了,游戏会打印一条消息说他们赢了,游戏结束,程序进入主类中的do-while循环,询问他们是否愿意再次玩。然而,如果不是这样,游戏将一直持续到这一点,或者如果已经使用了所有5次猜测,那么游戏将再次发送到do while循环,以便重新启动游戏,而不是简单地终止程序 问题是我不确定如何以及在何处构造if和else语句,以便这样做 任何帮助将不胜感激,如果我可以提供进一步的信息,以帮
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;
}
}
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"));
}
}
公共级刽子手{
私人猜;
私人整数生活;
私有字符串输出字,endMessage;
私有最终字符串隐藏字;
私有最终StringBuffer swapBuffer=新StringBuffer();
公众刽子手(){
letterGuess='';
numberlifes=5;
hiddenWord=“java”;
outputWord=“”;
endMessage=“”;
for(int i=0;i
主类
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;
}
}
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"));
}
}
import javax.swing.*;
公共类HangmanApp{
公共静态void main(字符串[]args){
猜字符;
整数生命;
字符串隐藏字、输出字、结束消息、重启游戏;
做{
刽子手我的刽子手=新刽子手();
showMessageDialog(null,“欢迎使用Java Hangman!”);
showMessageDialog(null,“在这个游戏中,一个单词将以星号打印给你-每个字母将在正确猜测后显示!”);
showMessageDialog(null,“游戏有5条生命,如果你猜得太多,游戏就会结束!”);
对于(int i=0;i<10;i++){
hiddenWord=myHangman.getHiddenWord();
numberlifes=myHangman.getnumberlifes();
showMessageDialog(null,“您当前拥有”+numberLifes+“生命!”);
letterGuess=JOptionPane.showInputDialog(null,“现在,请输入一个字母:”).charAt(0);
我的刽子手。setLetterGuess(letterGuess);
myHangman.compute();
outputWord=myHangman.getOutputWord();
showMessageDialog(null,“到目前为止的单词是:“+outputWord”);
}
numberlifes=myHangman.getnumberlifes();
showMessageDialog(null,“您已使用“+numberLives+”lives!”完成游戏);
restartGame=JOptionPane.showInputDialog(null,“您想再次玩吗?”);
}
while(restartGame.equalsIgnoreCase(“是”);
}
}
我是这么看的。调用myHangman.compute()之后代码>,您要检查玩家是否赢得游戏或耗尽生命。每个测试都是使用if
表达式的单独测试。没有else
部分,因为如果测试失败,游戏就会继续
如果识别出其中一个条件,则您希望显示一条消息,然后您希望退出当前游戏循环,以便询问用户是否要玩另一个游戏。方法是使用break
语句
一个问题是,在主游戏循环中,如果用户猜到了整个单词,您无法询问Hangman
类。要做到这一点,您可以将此方法添加到Hangman
类的底部:
public boolean getAllLettersFound() {
return outputWord.indexOf('*') < 0;
}
应该这样做。快乐编码 非常感谢你的工作,就像一个魅力,快乐的编码也!