在循环中将变量重置为新值

时间:2014-09-18 01:33:40

标签: java variables while-loop

我有一个小挂男式游戏,其中用户猜测一个字母并尝试确定字符串数组中的一个随机单词。问题是,一旦用户想要使用新的随机单词再次播放,旧单词仍然会与它一起出现?

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******** > 

问题发生在最后一行,为什么当它应该重置为一个新的随机字时,Bubble仍然显示为一个单词?

2 个答案:

答案 0 :(得分:4)

你的问题本身就回答了!你需要重置变量。尝试以下

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

答案 1 :(得分:2)

每次迭代后你都没有重置invisibleWord。

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;

}

} }