【问题标题】:hangman input won't be read不会读取刽子手输入
【发布时间】: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 是的,就是这样。

标签: java netbeans


【解决方案1】:

这只是一个疯狂的猜测,但那代码是不是错了?

public static void wordCondition(String guess)
{
    //check if letter is in word and substitutes the letter
    if (guess.contains(word))
        board[guess.indexOf(word)]=guess;
    else
        numGuesses--;
}

您正在检查输入字母是否包含单词。你应该反过来检查它:

public static void wordCondition(String guess)
{
    //check if letter is in word and substitutes the letter
    if (word.contains(guess))
        board[word.indexOf(guess)]=guess;
    else
        numGuesses--;
}

但这只会替换一个字母——如果有两个a,那么只会替换一个。你应该想办法做到这一点。

剧透

您可以使用它来替换所有字母。但是你真的应该使用 char[] 并为此改变你的方法:

public static void wordCondition(String guess)
{
    int position = 0;
    boolean found = false;
    for (char letter : word.toCharArray())
    {
        if (letter == guess.toCharArray()[0])
        {
            board[position]=guess;  
        }
        position++;
    }

    if (!found)
    {
        numGuesses--;
    }
}

【讨论】:

  • Ejaaaaaaaaaaaaaaactly,我的想法,关于这个话题 :-) +1
  • @nIcEcOw - 我们同时发布了它 - 我寻求答案而不是评论(虽然有一些疑问;))
  • 我试过了,但它总是作为错误返回
  • @CJNotyou 我已经编辑了我的答案。是:board[guess.indexOf(guess)]=guess; 应该是:board[word.indexOf(guess)]=guess;
  • 啊,现在可以用了,谢谢你的帮助,我现在只写一个字母
猜你喜欢
  • 2015-02-03
  • 2017-03-09
  • 2012-10-15
  • 2018-10-20
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多