gpt4 book ai didi

Java Hangman 项目无法正常停止

转载 作者:行者123 更新时间:2023-12-01 20:08:19 25 4
gpt4 key购买 nike

我正在尝试为我的大学制作一个 Hangman 程序,我需要一些帮助。经过一试,程序进展顺利。最后,您可以选择玩另一个游戏或停止程序,但它保持答案是"is",要求输入新单词。经过一番尝试后如何访问菜单。你能帮我吗?

import java.util.Scanner;

public class MainGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean found = true;
boolean playAgain = true;
int life = 8;

while (playAgain) {
System.out.println(" MAIN MENU ");
System.out.println("- Start a new Hangman Game (S) ");
System.out.println("- Exit (E) ");
System.out.println("Please enter your choice : ");

String answer = sc.nextLine().toUpperCase();

if (answer.equals("E")) {
playAgain = false;
} else {
System.out.println("Give Word");
String word = sc.nextLine();
char[] filler = new char[word.length()];
int i = 0;
while (i < word.length()) {
filler[i] = '-';
i++;
}

while (found) {
System.out.print("The Random Word is now : ");
System.out.println(filler);
System.out.print("You have " + life);
System.out.println(" Lives left");

System.out.print("Your Guess : ");
char guess = sc.next().charAt(0);
guess = Character.toUpperCase(guess);

System.out.println(guess);

int j = 0;
if (word.contains(guess + "")) {
System.out.println("The Guess is Correct !!");
for (j = 0; j < word.length(); j++) {
if (word.charAt(j) == guess) {
filler[j] = guess;
}
}
} else {
life--;
}

if (word.equals(String.valueOf(filler))) {
System.out.println("Congratulations You Won! Your Guessed Word is : " + word);
found = false;
}

if (life == 0) {
found = false;
System.out.println("Game Over.");
}
}
}
}
}
}

最佳答案

  • 使用大写单词
  • 在循环开始之前添加了 found = true
  • 替换了 sc.next() sc.nextLine()
    欲了解更多信息,请访问Understanding Scanner's nextLine(), next(), and nextInt() methods

            import java.util.Scanner;

    public class MainGame {

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    boolean found=true;
    boolean playAgain=true;
    int life=8;

    while (playAgain) {

    System.out.println(" MAIN MENU ");
    System.out.println ("- Start a new Hangman Game (S) ");
    System.out.println ("- Exit (E) ");
    System.out.println ("Please enter your choice : ");

    String answer = sc.nextLine().toUpperCase();

    if (answer.equals("E")) {
    playAgain=false;
    }
    else {

    System.out.println ("Give Word");

    String word = sc.nextLine();
    word = word.toUpperCase();
    char[] filler=new char[word.length()];
    int i =0;
    while (i<word.length()) {
    filler[i]='-';
    i++;
    }
    found = true;
    while (found) {

    System.out.print("The Random Word is now : ");
    System.out.println (filler);
    System.out.print ("You have "+life);
    System.out.println (" Lives left");

    System.out.print ("Your Guess : ");
    char guess = sc.nextLine().charAt(0) ;
    guess = Character.toUpperCase(guess);

    System.out.println (guess);

    int j=0;
    if (word.contains(guess+"")) {

    System.out.println ("The Guess is Correct !!");
    for (j=0;j<word.length();j++) {

    if(word.charAt(j)==guess) {
    filler[j]=guess;
    }
    }
    }
    else {
    life--;
    }

    if(word.equals(String.valueOf(filler))) {
    System.out.println ("Congratulations You Won! Your Guessed Word is : "+word);
    found=false;
    }

    if (life==0) {
    found=false;
    System.out.println ("Game Over.");}
    }
    }
    }
    }
    }

关于Java Hangman 项目无法正常停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47078669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com