Question

JAVA Develop a letter guessing game in this assignment using Java. Requirements: 1). When the game...

JAVA

Develop a letter guessing game in this assignment using Java. Requirements: 1). When the game is started, generate a random letter between A and Z; 2). Display a message "I have a secret letter (A to Z), can you guess what it is?"; 3). Read the user's answer; 4). Compare the user's answer and the random secret letter generated; 5). If the user answer is before the random secret letter in the alphabet, display "Incorrect. Try something bigger" and go to Step 2; 6). If the user answer is after the random secret letter in the alphabet, display "Incorrect. Try something smaller" and go to Step 2; 7). If the user answer is the same as the random secret letter, display "Well done. Want to play again (y/n)?"; 8). Read the user's answer. If the answer is 'y', go to Step 1. If the answer is 'n', go to Step 9; 9). Display "Thanks for playing the game. Goodbye!". The program stops.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.util.*;
public class LetterGuessing {

    public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
        String choice;
            do{          
        choice="y";
            System.out.println("I have a Secret Letter (A-Z) Can you guess it?");
            String user_letter=scan.nextLine();
            char input_letter=Character.toUpperCase(user_letter.charAt(0));
            char[] chars ={'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'};
            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};
            int user_int=0;
            int user_loop=0;
            char rand_letter=chars[(int)(Math.random()*26)];
            int comp_int=0;

                for(int i=0;i<chars.length;++i)
                {
                    if(rand_letter==chars[i])
                    {

                        comp_int=range[i];
                    }
                }
                //this loop is for getting the int value of user inputted letter
                for(char i:chars)
                {
                    if(input_letter==i)
                    {
                        user_int=range[user_loop];
                    }
                    ++user_loop;
                }


                 //test the entered letter of user
            if(input_letter==rand_letter)
            {
                System.out.println("Correct Guess");
                System.out.println("The letter is:"+rand_letter);
       System.out.println("Do you want to play again?(y/n)");
       choice=scan.next();
            }
            //test the entered letter of user if greater than computer input
            else if(user_int>comp_int)
            {
                System.out.println("Incorrect Guess");
                System.out.println("Try Something Smaller");
                System.out.println("The letter is:"+rand_letter);
            continue;
            }
            //test the entered letter of user if lesser than computer input
            else if(user_int<comp_int)
            {
                System.out.println("Incorrect Guess");
                System.out.println("Try Something higher");
                System.out.println("The letter is:"+rand_letter);
            continue;
            }
   }
        while(choice.equalsIgnoreCase("y"));


    }
  
}

Output:

Add a comment
Know the answer?
Add Answer to:
JAVA Develop a letter guessing game in this assignment using Java. Requirements: 1). When the game...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT