java - 在 Java 中隐藏输出文本

标签 java input output

我正在制作一个 Hangman 游戏,其中第一个用户输入一个要猜测的单词,第二个用户尝试猜测它。

基本上我想知道是否有办法隐藏第一个用户输入单词的位置,以便第二个用户无法向上滚动并看到它。下面是我的源代码。

package hangman;
public class Hangman 
{
public static void main(String[] args) 
{
    System.out.println("This game operates only in lowercase!");
    GameBoardClass myGame = new GameBoardClass();
    myGame.askForWord();
    while(myGame.gameActive())
    {
        myGame.guessLetter();
        myGame.checkForWinner();
    }
}
}


package hangman;
import java.util.*;
public class GameBoardClass
{
    private char[] GameBoard;
    private char[] CorrectWord;
    private boolean gameOnGoing = true;
    String mysteryWord;

    public boolean gameActive()
    {
        return gameOnGoing;
    }//checks if game should continue

    public void askForWord()
    {
        System.out.print("Please enter the word to be guessed by the opposing Player!: ");
        Scanner input = new Scanner(System.in);
        mysteryWord = input.nextLine();
        int i = mysteryWord.length();
        GameBoard = new char[i];
            for(int j = 0; j < i; j++)
            {
                char Blank = '_';
                GameBoard[j] = Blank;
                System.out.print(GameBoard[j] + " ");
            }
        CorrectWord = new char[i];
            for(int j = 0; j < i; j++)
            {
                char Blank1 = mysteryWord.charAt(j);
                CorrectWord[j] = Blank1;
            }
    }//end of Asking User for word and printing out blank spaces

    int Guesses = 7;
    public void guessLetter()
    {
        if(gameOnGoing = true)
        {

         int lol = 0;
         System.out.print("Guess a letter for this word!: ");
         Scanner input1 = new Scanner(System.in);
         String chickenNugget = input1.nextLine();
         char guessedLetter = chickenNugget.charAt(0);
            for(int i = 0; i < mysteryWord.length(); i++)
            {
                if(CorrectWord[i] == guessedLetter)
                {
                    GameBoard[i] = guessedLetter;
                    lol = lol + 1;
                }
            }
            if(lol == 0)
            {
                System.out.println("That was an incorrect guess!");
                Guesses = Guesses - 1;
                System.out.println("You have " + Guesses + " remaining.");
            }

            for(int i = 0; i < mysteryWord.length(); i++)
            {
                System.out.print(GameBoard[i] + " ");
            }
        }
    }//ends method asking the user to guess a letter

    public void checkForWinner()
    {
        String checkForWinnerString = "";
        String checkForWinnerString2 = "";
        for(int i = 0; i < mysteryWord.length(); i++)
        {
            checkForWinnerString += GameBoard[i];
            checkForWinnerString2 += CorrectWord[i];
        }
            if(checkForWinnerString.equals(checkForWinnerString2))
            {
                System.out.print("You've won the game!");
                gameOnGoing = false;
            }
        if(Guesses == 0)
        {
            System.out.print("You've lost the game! The word was " + mysteryWord + "\n");
            gameOnGoing = false;
        }
    }//end of checking for winner
}

此外,这里是逐行输出的示例。

This game operates only in lowercase!
Please enter the word to be guessed by the opposing Player!: Random
_ _ _ _ _ _ Guess a letter for this word!: a
_ a _ _ _ _ Guess a letter for this word!: n
_ a n _ _ _ Guess a letter for this word!:

谢谢大家!

最佳答案

java 中没有固有的方法来清除控制台。幸运的是,有多种方法可以解决这个问题,如here.所述。

GL!

关于java - 在 Java 中隐藏输出文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31815396/

相关文章:

python - 在规则中使用配置数据的语法

java - 动态设置图像到 ImageView

java - 快速获取 Java 输入

java - 将 Serializable 从一个 Activity 传递到另一个 Activity 不起作用

c++ - 在没有外部库的情况下在 C++ 中获取原始键盘和鼠标输入

input - Mootools DatePicker 手动输入

c - 找到一个c程序的输出

python - 使用 python pyodbc 捕获存储过程输出

java - java中如何删除附加的字符串

Java 套接字 - 自定义对象发送