猜字游戏java中的while循环

raogr8fs  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(450)

我正在尝试做一个字母猜谜游戏,用户需要猜a-z之间的字母,如果猜到的字母不正确,它会说字母比猜的高或低。我做了一个代码,它做的正确,但我不能添加循环,它将重复代码,直到用户猜到正确的字母。我是新手,所以一点解释和帮助将是非常好的。
这是我的密码:

import java.util.Scanner;

public class GuessTheLetter {

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

        System.out.println("Guess the Letter");
        String myLetter = scan.nextLine();
        char enteredLetter = Character.toUpperCase(myLetter.charAt(0));
        int[] range = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 };
        char[] characters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
        char randomLetter = characters[(int) (Math.random() * 26)];
        int userInputToInt = 0;

        int userInputControlLoop = 0;

        int computerInputToInt = 0;

        for (int i = 0; i < characters.length; ++i) {
            if (randomLetter == characters[i]) {
                computerInputToInt = range[i];
            }
        }

        for (char i : characters) {
            if (enteredLetter == i) {
                userInputToInt = range[userInputControlLoop];
            }
            ++userInputControlLoop;
        }

        if (enteredLetter == randomLetter) {
            System.out.println("Correct Guess");
            System.out.println("The letter is:" + randomLetter);
        }

        else if (userInputToInt > computerInputToInt) {
            System.out.println("Incorrect Guess");
            System.out.println("The letter is too high");
            System.out.println("The letter is:" + randomLetter);
        }

        else if (userInputToInt < computerInputToInt) {
            System.out.println("Incorrect Guess");
            System.out.println("The letter is too low");
            System.out.println("The letter is:" + randomLetter);

            scan.close();
        }

    }
}
hlswsv35

hlswsv351#

解决方案

下面是使用这个布尔变量更新的代码,就像前面提到的注解部分一样。
但是您还必须考虑添加try/catch/finally块,然后在finally块部分关闭扫描仪(finally将始终被调用)
因为你现在循环直到你有正确的猜测,你必须在循环之外创建随机变量,否则它会在每次迭代中创建一个新的随机字符。
另外,我去掉了这些控制循环,实际上使用char的一个很大的好处是可以将那里的ascii值与其他char进行比较。
在这里,我们只是比较大写字符,我们可以直接比较这些字符彼此 enteredLetter > randomLetter 例如。
例子:

enteredLetter = 'A';
randomLetter = 'D';
enteredLetter > randomLetter

此比较将检查ascii值a=65>d=69,以便a不大于d,并转到下一个条件。
此外,您还应该检查带有字符的有效输入,方法应该是character isleter()类的静态方法。也不要将输入作为字符串,而是直接将输入作为字符 char myLetter = scan.next().charAt(0); ```
package hall;

import java.util.Scanner;

public class GuessingGame {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    Boolean isGuessed = false;
    char[] characters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
            'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
    char randomLetter = characters[(int) (Math.random() * 26)];

    while (!isGuessed) {

        System.out.println("Guess the Letter");
        char myLetter = scan.next().charAt(0);
        if(!(Character.isLetter(myLetter))) {
            System.out.println("Invalid Input Try Again:");
        }else {

        char enteredLetter = Character.toUpperCase(myLetter);

        if (enteredLetter == randomLetter) {
            System.out.println("Correct Guess");
            System.out.println("The letter is:" + randomLetter);
            isGuessed = true;
        }

        else if (enteredLetter > randomLetter) {
            System.out.println("Incorrect Guess");
            System.out.println("The letter is too high");

        }

        else if (enteredLetter < randomLetter) {
            System.out.println(enteredLetter);
            System.out.println("Incorrect Guess");
            System.out.println("The letter is too low");

        }

        }
    }

}

}

du7egjpx

du7egjpx2#

这就是你想要的:

import java.util.Scanner;

public class GuessTheLetter {

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

        System.out.println("Guess the Letter:-\n");

        char randomLetter = (char) (Math.random() * 26 + 65); // logic to generate random letter

        char enteredLetter = 0; // initializing variable to store entered letter

        while (true) {// infinite loop till correct guess
            System.out.print("\nEnter your Guess : ");
            enteredLetter = Character.toUpperCase(scan.next().charAt(0)); // emter letter
            if (enteredLetter == randomLetter) {
                System.out.println("Correct Guess");
                System.out.println("The letter is:" + randomLetter);
                break;// exit loop if correct letter is guessed
            } else if (enteredLetter > randomLetter) // we can directly compare char like int
            {
                System.out.println("Incorrect Guess");
                System.out.println("The letter entered is too high");
            }

            else if (enteredLetter < randomLetter) // we can directly compare char like int
            {
                System.out.println("Incorrect Guess");
                System.out.println("The letter entered is too low");
            }

        }
        scan.close();
    }

}

解释在节目的评论里!

相关问题