Question: Write a program that randomly generates a character H or T, which represents head or tail. The program then prompts the user to enter a
Write a program that randomly generates a character H or T, which represents head or tail. The program then prompts the user to enter a guess and reports whether the guess is correct or incorrect.
- In order to generate a random character, first generate a random number, 0 or 1, using the Math.random() method, and then use a selection block to map 0 to H and 1 to T.
- For reading a character from a user, first read a string using a Scanner instance and then take the first character using charAt() method.
Write a program that randomly generates a character 'H' or 'T', which represents head or tail. The program then prompts the user to enter a guess and reports whether the guess is correct or incorrect. Hints: In order to generate a random character, first generate a random number, 0 or 1, using the Math.random() method, and then use a selection block to map 0 to 'H' and 1 to 'T'. For reading a character from a user, first read a string using a Scanner instance and then take the first character using charAt() method. Sample run: Guess head or tail (H or T)? H Correct! You can read my mind. Another run: Guess head or tail (H or T)? H Sorry, it was tails
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
