Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Rafał Stasiak
Rafał Stasiak
3,763 Points

Java Class scope

Hello team,

I get stuck with below code:

class NewGame { GuessAttempts GuessAttempts = new GuessAttempts(); private String WordToGuess; private String CorrectGuesses; private String misses; int guessAttempts; int MAX_Guesses = 9;

public NewGame(String WordToGuess) { this.WordToGuess = WordToGuess; CorrectGuesses = ""; misses = ""; guessAttempts = 0; }

public boolean UserGuessing(char letter) {
    boolean isHit = WordToGuess.indexOf(letter) != -1;
    if (isHit) {
        CorrectGuesses += letter;
    } else {
        misses += letter;
    }
    guessAttempts =+ 1;
    return isHit;
}
if (guessAttemts = MaxGuesses) {
System.out.println("Sorry you used all your nine chances. Game Over.");
}

I just cant iniciate

if (guessAttemts = MaxGuesses) { System.out.println("Sorry you used all your nine chances. Game Over."); }

guessAttemts and MaxGuesses are marked on red, but why?

4 Answers

Steven Parker
Steven Parker
231,248 Points

It's a bit hard to read the unformatted code, but it would appear that both guessAttemts and MaxGuesses are both being referenced without having been declared.

In future, when posting code use Markdown formatting.

Rafał Stasiak
Rafał Stasiak
3,763 Points

I menage to resolve this. After todays work i wll send formated edited working code :)

Rafał Stasiak
Rafał Stasiak
3,763 Points

Ok, so i will try to reformat code display :

class NewGame { GuessAttempts GuessAttempts = new GuessAttempts(); 
private String WordToGuess; 
private String CorrectGuesses;
 private String misses;
 int guessAttempts;
 int MAX_Guesses = 9;

public NewGame(String WordToGuess) { this.WordToGuess = WordToGuess; CorrectGuesses = ""; misses = ""; guessAttempts = 0; }

public boolean UserGuessing(char letter) {
    boolean isHit = WordToGuess.indexOf(letter) != -1;
    if (isHit) {
        CorrectGuesses += letter;
    } else {
        misses += letter;
    }
    guessAttempts =+ 1;
    return isHit;
}
if (guessAttemts = MaxGuesses) {
System.out.println("Sorry you used all your nine chances. Game Over.");
}
Rafał Stasiak
Rafał Stasiak
3,763 Points

Upper code was reformated and rewrited I only checked if I properly udnerstood posting block of code