[英]Hiding output text in Java
我正在制作一個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中的控制台。 幸運的是,有多種解決方法,如此處所述。
GL!
一種按要求執行的方法:
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.