maybe someone can take a look and help me figure out!
so i need to
represent my secret word if form of questions marks The disguised word is <?????????> like this for instance, then i need to enter a letter and determine if this character is in the secret word, if its, the disguised word is update to show the position of that letter.
its actually works but when i compile it.. it does represent my secret word in disguised form..
thats what i've got right now
package question3;
import java.util.Scanner;
public class HidWord {
Scanner kb=new Scanner(System.in);
String secretPhrase, disguisedPhrase,guess;
int correctGuess,inCorrectGuess,totalGuess;
public HidWord(String newPhrase){
secretPhrase=newPhrase;
}
//method to represent disguised form
public void disguisedPhrase(){
disguisedPhrase="";
for(int i=0;i<secretPhrase.length();i++){
disguisedPhrase+="?";}
}
public String getDisguisedPhrase(){return disguisedPhrase; }
public String toString(){return disguisedPhrase;}
public void guess(){
System.out.println("Guess a letter: ");
guess=kb.next().toLowerCase();
//checkGuess(guess);
totalGuess++;
}
//check if the character that user input is in the secret word
public void checkGuess(char guess){
char current;
boolean guessCorrectly=false;
for(int i=0; i<secretPhrase.length(); i++){
current=secretPhrase.charAt(i);
if (current==guess)
{
guessCorrectly=true;
if(i<secretPhrase.length())
{disguisedPhrase=disguisedPhrase.substring(0,i)+current+disguisedPhrase.substring(i+1);}
else
{
disguisedPhrase=disguisedPhrase.substring(0,i)+current; }
}
}
if(guessCorrectly)
{
correctGuess++;
}
else
{inCorrectGuess++;}
}
public boolean isPhraseComplete(){
return disguisedPhrase.equalsIgnoreCase(secretPhrase);
}
public void Output(){
System.out.println("The disguised word is :"+ getDisguisedPhrase());
//System.out.println("Guess a letter:");
}
public static void main(String[] args)
{
HidWord test=new HidWord("happiness");
//System.out.println(test.getDisguisedPhrase());
// test.HidWord("");
test.Output();
//test.getDisguisedPhrase();
test.guess();
test1.disguisedPhrase();
}
}
the game keep going until the word is complete.
in the end it should be something like this:
The disguised word is <?????????>
Guess a letter:a
Guesses made: 1 with 0 wrong
The disguised word is <?a???????>
Guess a letter:e
Guesses made: 2 with 0 wrong

New Topic/Question
Reply



MultiQuote







|