信猜猜游戏Java

时间:2017-03-28 23:46:26

标签: java char

我一直在为字母(a-z)制作java猜谜游戏!然而,我通过使用数字1-26完美地创建了游戏,但我无法弄清楚如何将每个整数转换为字母,即a = 1,b = 2,.... z = 26!

我希望用户尝试猜测字母而不是数字,但我无法锻炼如何做到这一点!

(我知道如何生成随机字符,但我无法实现并将其正确链接到游戏中的每个整数)

Random r = new Random();

char targetLetter = (char)(r.nextInt(26) + 'a');       

任何帮助将不胜感激!如果需要,我可以显示我的代码

public class Stack {

    public static void main(String[] args) {

        Random rand = new Random(); //This is were the computer selects the Target

        int guess;
        int numGuesses = 0;
        int Target;
        String userName;
        String playagain;
        boolean play = true;
        int session = 0;
        int sessions = 0;
        int bestScore = 0;

        Scanner consoleIn = new Scanner(System.in);
        Scanner name = new Scanner(System.in);

        System.out.println("Hello! Please enter your name:\n"); //This is were the user enters his/her name
        userName = name.nextLine();

        System.out.println("Hello " + userName + " :) Welcome to the game!\n");


        while (play = true) {
            session++;
            Target = rand.nextInt(26) + 1;
            System.out.println("Guess a number between 1 and 26? You will have 5 attempts to guess the correct number"); //This is where the computer asks the user to guess the number and how many guesses they will have

            do {
                guess = consoleIn.nextInt();
                numGuesses++;

                if (guess > 26)
                    System.out.println("Error! Above MAXIMUM range");
                else if (guess <= 0)
                    System.out.println("Error! Below MINIMUM range");
                else if (guess > Target)
                    System.out.println("Sorry! Your guess was too high! :)"); //This is to help the player get to the answer
                else if (guess < Target)
                    System.out.println("Sorry! Your guess was too low! :)"); //This is to help the player get to the answer
            } while (guess != Target && numGuesses < 5);

            if (guess == Target) {
                System.out.println("Congratulations " + userName + ", it took you " + numGuesses + " attempts to guess correctly!"); //This tells the player that they got the correct answer and how many attempts it took
                sessions++;
            } else {
                System.out.println("Sorry " + userName + ", You've used up all of your guesses! The correct answer was " + Target + "!");  //This tells the player that they failed to find the number and then tells them what the correct answer
            }
            {
                Scanner answer = new Scanner(System.in);
                System.out.println("Would you like another GO " + userName + "? [Y/N]");//This asks the player if they would like to play again
                playagain = answer.nextLine();
                if (playagain.equalsIgnoreCase("Y")) {//This is what happens if the player opts to play again
                    play = true;
                    numGuesses = 0;
                } else if (playagain.equalsIgnoreCase("N")) {//This is what happens if the player opts to exit the game
                    play = false;
                    System.out.println("Thanks for playing " + userName + "! :) Please come back soon!");
                    System.out.println("You had  " + session + " Goes");
                    System.out.println("The number of times you guessed correctly: " + sessions + "");
                    break;
                }
            }
        }
    }
}

4 个答案:

答案 0 :(得分:0)

使用字符数组

char[] chars = ['A','B','C'...];

并使用随机数映射到每个字符

char targetLetter = chars[r.nextInt(26)];

答案 1 :(得分:0)

 public static void main(String args[])
        {
            Scanner scan = new Scanner(System.in);      
            System.out.println("Guess the Letter");
            String myLetter=scan.nextLine();
                //get the letter of myLetter variable then convert to Uppercase
            char enteredLetter=Character.toUpperCase(myLetter.charAt(0));
                    //26 only because the characters array starts with index 0
            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'};
                    //I had created a parrallel array symbolizing int value of each letter
            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};
            //this variable convert user input to one of the array element of range
            int userInputToInt=0;
            //this variable is for knowing what int[] range array element must the value of userInputToInt fall
            int userInputControlLoop=0;
            char randomLetter=characters[(int)(Math.random()*26)];
            // get the random input of computer convert it to int
            int computerInputToInt=0;

                //this loop is for getting the int value of randomLetter input by the computer
                for(int i=0;i<characters.length;++i)
                {
                    if(randomLetter==characters[i])
                    {

                        computerInputToInt=range[i];
                    }
                }
                //this loop is for getting the int value of user inputted letter
                for(char i:characters)
                {
                    if(enteredLetter==i)
                    {
                        userInputToInt=range[userInputControlLoop];
                    }
                    ++userInputControlLoop;
                }


                 //test the entered letter of user
            if(enteredLetter==randomLetter)
            {
                System.out.println("Correct Guess");
                System.out.println("The letter is:"+randomLetter);
            }
            //test the entered letter of user if greater than computer input
            else if(userInputToInt>computerInputToInt)
            {
                System.out.println("Incorrect Guess");
                System.out.println("The letter is too high");
                System.out.println("The letter is:"+randomLetter);
            }
            //test the entered letter of user if lesser than computer input
            else if(userInputToInt<computerInputToInt)
            {
                System.out.println("Incorrect Guess");
                System.out.println("The letter is too low");
                System.out.println("The letter is:"+randomLetter);
            }

        }

答案 2 :(得分:0)

使用与随机字符相同的方法。假设您将猜测的字符作为名为“guess”的int变量,并且它具有值1-26对应的A-Z:

Random r = new Random();
char targetLetter = (char)(r.nextInt(26) + 'a');

...

int guess = ...
char guessChar = (char)guess + 'a';
if (guessChar == targetLetter) { 
    System.out.println("Correct!");
} else {
    System.out.println("Guess again!")
}

答案 3 :(得分:0)

您可以使用此方法实现它:

1-使用您想要的字符创建字符串alphabet

2-将字母大小声明为n变量,它将控制随机生成器范围。

3- alphabet.charAt(random.nextInt(n))是字母表中的随机字符。

程序代码将是:

String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int n = alphabet.length();

Random r = new Random();  

System.out.println(alphabet.charAt(r.nextInt(n)));

希望有助于解决您的问题。

相关问题