Help with java char Array
Working on a Hangman Game. Trying to add a guess(char variable) into char array and display a list of guessed letters. Please help this piece code isn't working
Code java:
while(guessedWord.toString().equals(test.toString( )) == false && errors < 5)
{
System.out.print("Guess Letter: ");
Scanner scan = new Scanner(System.in);
guess = scan.next().charAt(0);
char[] guessedLetters = new char [10]; //THE PROBLEM
System.out.println(guessedLetters); //THE OTHER PROBLEM
//System.out.println("Letters Already Used: "+ lettersGuessed[0]);
for(int i = 0; i < guessedWord.length(); i++){ //check if letter has been guessed
if(guessedWord.charAt(i) == guess)
{
test.setCharAt(i, guess);
System.out.println(test);
}
}
if(word.indexOf(guess) == -1 ) //if letter not in secret then errors goes up by 1 and while loop continues
{
errors++;
}
if(guessedWord.toString().equals(test.toString( )) == true)
{
score = (5 - errors);
cum += score;
System.out.println("\nYou win !!!");
System.out.println("Your cumlative score is: " + cum);
initGame();
}
if(errors == 5)
{
String name;
Scanner input = new Scanner(System.in);
System.out.println("\nYou lose !!!");
System.out.println("Your score is: " + cum);
System.out.print("Please enter your name: ");
name = input.nextLine();
myWord.setSaveScore(name, cum);
cum = 0;
initGame();
}
Re: Help with java char Array
If I were you I'd start with some pseudo code and then start programming it and if you run into any errors it would be a lot easier to help.
Something like:
While loop to keep the user entering until ran out of guesses and you could use a counter to keep track of number of guesses
Going to need a scanner to get the input from keyboard
int x; // storage variable
For loop to access all the index's in the array
x = Integer.parseInt(s); (Trying to give a hint)
Let the index of the loop equal x
Hope this helps.
Re: Help with java char Array
static void guess(String secret, int times) {
// read character from console
Scanner reader = new Scanner(System.in);
int secretLen = secret.length();
int timesCount = 0, index = 0;
while (timesCount++ < times) {
System.out.println("Enter the letter you want guess: ");
char incomingLetter = reader.next().charAt(0);
char targetLetter = secret.charAt(index);
if (incomingLetter == targetLetter) {
System.out.println("You got it, please guess the next");
index++;
if (index >= secretLen)
break;
} else {
System.out.println("Wrong, the letter is not matched, repeat!");
}
}
if (index >= secretLen) {
// guess operation succeed
System.out.println("You win");
}
}
This is what I do to guess the letter. And the print things is changeable for you to fulfill your specific target!
Re: Help with java char Array
Ah I'm glad to see you revised your post haha! :)
I don't think you can just print guessedLetters like you have.
Try this:
Code Java:
import java.util.Arrays;
.
.
.
System.out.println( Arrays.toString( guessedLetters ) );
Re: Help with java char Array
Doesn't really answer my intended question. If you have a look at my code above. Im having problems when displaying all the guessed letters. As in when user guesses it stores in array then displays it