gpt4 book ai didi

java - 在 Java 中隐藏输出文本

转载 作者:太空宇宙 更新时间:2023-11-04 13:34:45 24 4
gpt4 key购买 nike

我正在制作一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com