简体   繁体   English

在Java中隐藏输出文本

[英]Hiding output text in Java

I am making a Hangman game, in which the first user inputs a word to be guessed, and the second user, well, tries to guess it. 我正在制作一个Hangman游戏,其中第一个用户输入要猜的单词,第二个用户尝试猜它。

Basically I want to know if there is a way to hide where the first user inputted the word, so that the second user can't scroll up and see it. 基本上,我想知道是否有一种方法可以隐藏第一个用户输入该词的位置,以便第二个用户无法向上滚动并看到它。 Here is my source code below. 这是我下面的源代码。

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
}

In addition, here is an example of what the output might be line-by-line. 此外,这是输出可能逐行输出的示例。

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!:

Thanks everyone! 感谢大家!

There is no inherent way to clear the console in java. 没有固有的方法可以清除Java中的控制台。 Fortunately, there are various ways to work-around it as described here. 幸运的是,有多种解决方法,如此处所述

gl! GL!

A way to perform as requested: 一种按要求执行的方法:

public final static void clearConsole()
{
try
{
    final String os = System.getProperty("os.name");

    if (os.contains("Windows"))
    {
        Runtime.getRuntime().exec("cls");
    }
    else // basically, linux
    {
        Runtime.getRuntime().exec("clear");
    }
}
catch (final Exception e)
{
    //  Handle any exceptions.
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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