\" The code must be written in Loop and Libarary Methods \" There is a \"fun\" c
ID: 3532167 • Letter: #
Question
" The code must be written in Loop and Libarary Methods "
There is a "fun" children's game where one child thinks of a "common phrase", then the second child repeatedly makes guesses as to the letters that it contains.
You are to write a Java program that:
1. Prompts a user to enter a "common phrase", and reads it into a variable using Scanner.nextLine() - under the assumption that the phrase consists of nothing but undercase letters and spaces.
2. A counter is set to zero.
3. The following is than repeated until the entire "common phrase" is revealed:
a) The "common phrase" is displayed with all of the letters (that have not yet been correctly guessed) replaced with the ? character and all of the letters that have been correctly guessed displayed as is.
b) Using a user input validation loop, the second user is prompted to enter a single lowercase letter guess.
c) A counter is incremented.
4. Once the "common phrase" is revealed, you must also report the total number of valid guesses that were made to do so.
Hints:
Store the letters that have been correctly guessed in an initially empty ("") String object, using the concatenation operator (+=).
A fair amount of partial credit shall be given for just being able to respond with "correct" / "incorrect" for each second user guess.
Sample run(s):
Please enter the phrase to guess at : who do you love
Common Phrase
-------------
??? ?? ??? ????
Enter a lowercase letter guess : f
Common Phrase
-------------
??? ?? ??? ????
Enter a lowercase letter guess : o
Common Phrase
-------------
??o ?o ?o? ?o??
Enter a lowercase letter guess : s
Common Phrase
-------------
??o ?o ?o? ?o??
Enter a lowercase letter guess : w
Common Phrase
-------------
w?o ?o ?o? ?o??
Enter a lowercase letter guess : h
Common Phrase
-------------
who ?o ?o? ?o??
Enter a lowercase letter guess : d
Common Phrase
-------------
who do ?o? ?o??
Enter a lowercase letter guess : e
Common Phrase
-------------
who do ?o? ?o?e
Enter a lowercase letter guess : y
Common Phrase
-------------
who do yo? ?o?e
Enter a lowercase letter guess : u
Common Phrase
-------------
who do you ?o?e
Enter a lowercase letter guess : l
Common Phrase
-------------
who do you lo?e
Enter a lowercase letter guess : v
Common Phrase
-------------
who do you love
Was guessed in 11 guesses.
Explanation / Answer
import java.util.*; public class Guess { public static void main(String[] arg) { String prompt,guess; int counter=0,i,id=0; char[] prmt=new char[50]; char[] a=new char[2]; Scanner s=new Scanner(System.in); System.out.print("Enter the common phrase"); prompt=s.nextLine(); char[] str=new char[prompt.length()]; System.out.print("Common phrase"); for(i=0;i
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.