简体   繁体   中英

Resetting a variable to new value in loop

I have a small hang man type game in which the user guesses a letter and tries to determine one of the random words from a string array. The issue is that once the user wants to play again with a new random word, the old word still shows up along side with it ?

import java.util.Random;
import java.util.Scanner;
import java.lang.Character;

public class Game
{
    public static void main(String[] args)
    {
        String[] words = {"Computer", "Software" , "Program" ,
                          "Algorithms", "Unix" , "Mathematics" ,
                          "Quick" , "Bubble" , "Graph" };

        Random rnd = new Random();
        Scanner sc = new Scanner(System.in);

        int index;
        String word = "", 
        invisibleWord = "", 
        newString = "", 
        guess,
        status;

        while(true)
        {
            index = rnd.nextInt(words.length);
            word = words[index];

            for(int i = 0; i < word.length(); ++i)
                invisibleWord += "*";

            while(true){
                System.out.print("(Guess) Enter a letter in word "
                                   + invisibleWord + " > ");

                guess = sc.nextLine();

                for(int j = 0; j < word.length(); ++j)
                    if(guess.toUpperCase().equals(Character.toString(word.charAt(j)).toUpperCase())){
                        if(j == 0){
                        newString = invisibleWord.substring(0,j) + guess.toUpperCase() + invisibleWord.substring(j + 1);
                        invisibleWord = newString;
                        } else {
                        newString = invisibleWord.substring(0,j) + guess + invisibleWord.substring(j   + 1);
                        invisibleWord = newString;
                        }
                }

                if(invisibleWord.contains("*")) continue;
                else break;

        }
                    System.out.println("The word is " + word);
                    System.out.print("Do you want to guess another word ? " +
                                       "Enter y or n > ");
                    status = sc.nextLine();
                    if(status.toUpperCase().equals("Y")) continue;
                    else if(status.toUpperCase().equals("N")) break;

    }


}
}

SAMPLE RUN:

(Guess) Enter a letter in word ****** > a
(Guess) Enter a letter in word ****** > e
(Guess) Enter a letter in word *****e > i
(Guess) Enter a letter in word *****e > o
(Guess) Enter a letter in word *****e > u
(Guess) Enter a letter in word *u***e > b
(Guess) Enter a letter in word Bubb*e > l
The word is Bubble
Do you want to guess another word ? Enter y or n > y
(Guess) Enter a letter in word Bubble******** > 

The problem occurs in the last line, why is Bubble still showing up as a word when it is supposed to be reset to a new random word ?

your question itself answers it! you need to reset the variable. try following

if(status.toUpperCase().equals("Y"))
{
       invisibleWord = "";
       continue;
}

You are not resetting invisibleWord after each iteration.

while(true)
    {
        invisibleWord="";
        index = rnd.nextInt(words.length);
        word = words[index];

        for(int i = 0; i < word.length(); ++i)
            invisibleWord += "*";

        while(true){
            System.out.print("(Guess) Enter a letter in word "
                               + invisibleWord + " > ");

            guess = sc.nextLine();

            for(int j = 0; j < word.length(); ++j)
                if(guess.toUpperCase().equals(Character.toString(word.charAt(j)).toUpperCase())){
                    if(j == 0){
                    newString = invisibleWord.substring(0,j) + guess.toUpperCase() + invisibleWord.substring(j + 1);
                    invisibleWord = newString;
                    } else {
                    newString = invisibleWord.substring(0,j) + guess + invisibleWord.substring(j   + 1);
                    invisibleWord = newString;
                    }
            }

            if(invisibleWord.contains("*")) continue;
            else break;

    }
                System.out.println("The word is " + word);
                System.out.print("Do you want to guess another word ? " +
                                   "Enter y or n > ");
                status = sc.nextLine();
                if(status.toUpperCase().equals("Y")) continue;
                else if(status.toUpperCase().equals("N")) break;

}

} }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM