【发布时间】:2014-09-04 21:45:24
【问题描述】:
我在 java 中有一个刽子手任务,我的大部分程序都可以工作,除非它尝试读取输入并保存它。我只使用字符串,因为我不想将 char 转换为字符串
我的主要:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
boolean active = true;
do
{
startGame();
do
{
System.out.println("You have "+(numGuesses)+" guesses left");
drawBoard();
System.out.println();
System.out.println("Please enter your next guess: ");
String mainGuess = input.nextLine();
if ("stop".equals(mainGuess))
currentState = End;//stops game if player chooses
else
{
wordCondition(mainGuess);
winLose();
}
}
while (currentState == Play);
if (currentState == Win)
System.out.println("Coongradulations you won!");
else if (currentState == Lose)
System.out.println("Sorry, You lost");
System.out.println("Would you like to play again? 1)yes 2)no");
int answer=input.nextInt();
active = (answer==1);
}
while (active);//creates a new game as much as user wants
}
和我的问题块
public static void wordCondition(String guess)
{
if (guess.contains(word))//check if letter is in word and substitutes the letter
{
board[guess.indexOf(word)]=guess;
total++;
}
还有我的绘图块
public static void drawBoard()
{
System.out.println("Current word:");
for(int i=0;i<word.length();i++)
System.out.print(board[i]);
}
例如这个词是“名字” 我想要什么
你还有 7 个猜测 当前单词:_ _ _ _ 请输入您的下一个猜测: 一个
你还有 7 个猜测 当前单词:_ a _ _ 请输入您的下一个猜测:
我得到了什么
你还有 7 个猜测 当前单词:_ _ _ _ 请输入您的下一个猜测: 一个
你还有 6 个猜测 当前单词:_ _ _ _ 请输入您的下一个猜测:
或类似的东西,格式有点不对 请帮忙:)
【问题讨论】:
-
是时候学习如何使用 IDE 的调试器了。
-
你的“问题块”产生了什么问题?
-
board在哪里以及如何定义? -
if (guess.contains(word))条件真的正确吗?在我看来,应该是,if (word.contains(guess))。 -
@nIcEcOw 是的,就是这样。