#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Unhappy Simple Problem in my code... Help


    This is sample of my code. I need be able to have a cumulative score after the user plays x number of times. But keep getting same answer.

    import java.util.Scanner;
    public class Hangman
    {

    public static void main(String[] args)
    {
    System.out.println("Welcome to Hangman... v1.0 - Designed Ashley Bwanya");
    initGame();
    }

    public static void initGame() {


    int errors = 0;
    int cumScore = 0,score;
    String word;
    String displayHidden = "";
    String used = "";

    Game myWord = new Game();
    char guess;


    word = myWord.getWord();



    for (int i = 0; i < word.length(); i++)
    {
    displayHidden += "*";
    }

    StringBuilder guessedWord = new StringBuilder(word);
    StringBuilder test = new StringBuilder(displayHidden);



    System.out.println("This is the secret word you have to guess: \n");
    System.out.println(test+ "\n");


    while(guessedWord.toString().equals(test.toString( )) == false && errors < 5)
    {
    char[] lettersGuessed = new char[10] ;
    System.out.print("Guess Letter: ");
    Scanner scan = new Scanner(System.in);
    guess = scan.next().charAt(0);

    //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);
    cumScore += score;
    System.out.println("You win !!!");
    System.out.println("Your cumlative score is: " + cumScore);
    initGame();
    }

    if(errors == 5)
    {
    score = (5 - errors);
    String name;
    Scanner input = new Scanner(System.in);
    System.out.println("You lose !!!");
    System.out.println("Your score is: " + score);
    System.out.print("Please enter your name: ");
    name = input.nextLine();
    myWord.setSaveScore(name, score);

    }


    }



    }

    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    How many places have you posted this question?

    Did you look at and respond to the responses made on the other sites?

    Also at http://www.javaprogrammingforums.com/java-theory-questions/19100-basic-java-calculating-cumulative-score-hangman-help.html
    and
    http://www.javaprogrammingforums.com...-sum-code.html
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    try to follow his suggestion. i'll also try to search other site that can help and solve your problem..
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0
    Try looking into static fields. You can declare a static field for an Object which allows one value for all objects you initialize. You can then simply do a count each time (E.g) the Game object is called.

    Also looking at your code I notice this line:

    if(guessedWord.toString().equals(test.toString( )) == true)

    You are using unnecessary code. The if statement automatically continues if the boolean expression is true, you don't need to specify this.

    You should use:
    if(guessedWord.toString().equals(test.toString( )))

IMN logo majestic logo threadwatch logo seochat tools logo