为猜字游戏创建布尔值.卡住了



我是java新手,我正在尝试创建一个基本的绞刑游戏,我已经为回合创建了方法,但我遇到了创建一个布尔值,将游戏转到下一轮(玩家失去一次尝试猜一个字母,如果他把错误的字母;他只会犯错10次;如果猜到了,他就继续游戏)。这是我的代码到目前为止,我甚至尝试排除布尔的if/else语句,但它似乎非常愚蠢的我。任何帮助都是有用的。谢谢你。

import java.util.Random;
import java.util.Scanner;
public class assignmenTwo {
    public static void main(String[] args) {
        welcomeMessage();
        String myWord = randomizeWord();
        char[] hiddenWord = hideWord(myWord);
        printArr(hiddenWord);
        char guess = userLine();
        game(guess, myWord);
        //
        // indexOfGuessed(guess,myWord,hiddenWord);
        // printArr(hiddenWord);
    }
    public static void game (char userGuess, String word) {
        int attempts = 10;
        while (attempts > 0) {
            round();
            for (int i = 0; i < word.length(); i++) {
                if (userGuess != word.charAt(i)) {
                    attempts--;
                    System.out.println("You have"+attempts+"attempts. Try again!");
                }
            }
        }
    }
    public static void round () {
        String myWord = randomizeWord();
        char[] hiddenWord = hideWord(myWord);
        printArr(hiddenWord);
        char guess = userLine();
        indexOfGuessed(guess,myWord,hiddenWord);
        printArr(hiddenWord);
        return;
    }

    public static void welcomeMessage() {
        String welcome = "Welcome";
        System.out.println(welcome);
    }// say hi
    public static String randomizeWord() {
        Random r = new Random();
        String[] dictionary = {"hello", "dear", "Universe"};
        int wordIndex = r.nextInt(dictionary.length);
        String myWord = dictionary[wordIndex];
        return myWord;
    }
    public static char[] hideWord(String myString) {
        char[] hidWord = new char[myString.length()];
        for (int i = 0; i < myString.length(); i++) {
            hidWord[i] = '*';
        }
        return hidWord;
    }
    public static char userLine() {
        Scanner in = new Scanner(System.in);
        System.out.println("nYour word is above. Guess a letter!");
        char userInput = in.next().charAt(0);
        return userInput;
    }
    public static String stars(String word) {
        String stars = "";
        for (int i = 0; i < word.length(); i++) {
            stars += '*';
        }
        return stars;
    }
    public static void  printArr (char [] arr) {
        System.out.println();
       for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
    }
    public static void indexOfGuessed(char userGuess, String word, char[] hiddenWord) {
        for (int i = 0; i < word.length(); i++) {
            if (userGuess == word.charAt(i)) {
                hiddenWord[i] = userGuess;
            }
        }
        return;
    }
}

另一件事(你关于布尔返回值的问题似乎回答了):每次调用round()时,您都会创建一个新的String myWord

此字符串仅对round()方法可见,而与您在主方法中初始化的String myWord不同。

我建议你把myWord作为一个全局变量,设置一次,如果需要,让其他方法引用这个变量。

就像现在一样,每次调用round()只是从你的字典中获得一个新的随机单词。

我还想说,如果你不把所有东西都变成静态的,那就更好了,因为eclipse(或其他IDE)告诉你,你不能在静态主方法中创建非静态引用:

String myWord;
    public static void main(String[] args) {
        AssignmenTwo a2 = new AssignmenTwo();
        a2.runGame();
        //
        // indexOfGuessed(guess,myWord,hiddenWord);
        // printArr(hiddenWord);
    }
    public void runGame(){
        welcomeMessage();
        myWord = randomizeWord();
        char[] hiddenWord = hideWord(myWord);
        printArr(hiddenWord);
        char guess = userLine();
        game(guess, myWord);
    }
    public void game(char userGuess, String word) {
        int attempts = 10;
        while (attempts > 0) {
            round();
            for (int i = 0; i < word.length(); i++) {
                if (userGuess != word.charAt(i)) {
                    attempts--;
                    System.out.println("You have" + attempts + "attempts. Try again!");
                }
            }
        }
    }
    public void round() {
        char[] hiddenWord = hideWord(myWord);
        printArr(hiddenWord);
        char guess = userLine();
        indexOfGuessed(guess, myWord, hiddenWord);
        printArr(hiddenWord);
        return;
    }  ...

您可以像这样更改IndexOfGuessed函数,这样它将返回搜索的状态:

public static void findGuessed(char userGuess, String word, char[] hiddenWord) {
    boolean found = false;
    for (int i = 0; i < word.length(); i++) {
        if (userGuess == word.charAt(i)) {
            hiddenWord[i] = userGuess;
            found = true;
        }
    }
    return found;
}

然后你可以让round也返回一个布尔值,表示它是否赢了:

public static void round () {
    String myWord = randomizeWord();
    char[] hiddenWord = hideWord(myWord);
    printArr(hiddenWord);
    char guess = userLine();
    return findGuessed(guess, myWord, hiddenWord);
}

然后在game中你可以检查一下:

boolean won = round();
if (won) {
   ...
}

而不是:

while (attempts > 0) {
    round();
    for (int i = 0; i < word.length(); i++) {
        if (userGuess != word.charAt(i)) {
            attempts--;
            System.out.println("You have"+attempts+"attempts. Try again!");
        }
    }
}

你应该用一个布尔值来定义游戏是否结束,如:

boolean over = false;
while(!over){
    over = round();
}

其中round()将调用函数来捕获用户的建议,将其与秘密词进行比较并减少attempts的值(可以是类的属性或参数传递的变量),并在提示用户结果后返回游戏结束。

当离开循环时,你可以询问用户是否希望再次尝试并调用setup函数来重新启动游戏。

最新更新