Strings, if statements, while loops
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 th assumption that the phrase consists of nothing but undercase letters and spaces.
2. The following is than repeated until the entire "common phrase" is revealed:
* 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.
* Using a user input validation loop, the second user is prompted to enter a single lowercase letter guess.
Hints:
* Store the letters that have been correctly guessed in an initially empty ("") String object, using the concatenation operator (+=).
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
Re: Strings, if statements, while loops
This is what I have on eclipse now:
import java.util.Scanner;
public class cs201
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String guess;
String commonPhrase = "who do you love";
Scanner.nextLine();
System.out.print("Please enter the phrase to guess at : who do you love");
}
}
obviously i'm not very far at all. I just need some direction because we haven't had assignments like this one before
Re: Strings, if statements, while loops
Professor gave us a few lines of code that should be used in this program so I put it all together just to have it all there. It's certainly not in order towards the end but I don't know how exactly to use this all for the program. this is what I have on my screen now:
import java.util.Scanner;
/******************************
* Take Home Exam
* Matt Taylor
*
* Guessing that phrase that pays the A
********************************/
public class TakeHomeExam
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String guess= "";
System.out.print("Please enter the phrase to guess at : ");
String commonPhrase= stdIn.nextInt();
for (int i=0; i < commonPhrase.length(); ++i);
System.out.print (commonPhrase.chartAt(i));
commonPhrase.charAt(i)==
guess.charAt(0)
}
}