java Java作业刽子手

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12306955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:23:34  来源:igfitidea点击:

Java homework hangman

java

提问by user519670

So for a class we are having to make a hangman game that can take a user input for a word and then have another person solve for it. It has to be able to recognize multiple repeating letters in the word or I would be done. Below is my code, it works great until I remove the break statement in my checkformatch method so that it goes past the initial finding of a letter. With the break in there it never finds the second third etc repeated letters, without it, it returns that each letter that is not the letter searched is a miss and reduces my life count. What I'm needing is some hints on how to search my array for the letter that is inputted as a guess and return their index positions in the array without it thinking each character in the array that is not the one guessed is a wrong input. Thank you in advance.

因此,对于一个班级,我们必须制作一个刽子手游戏,该游戏可以接受用户输入的单词,然后让另一个人解决它。它必须能够识别单词中的多个重复字母,否则我就完了。下面是我的代码,它工作得很好,直到我删除了 checkformatch 方法中的 break 语句,以便它超过最初找到的字母。随着那里的中断,它永远找不到第二个三分之一等重复的字母,没有它,它会返回不是搜索到的字母的每个字母都是一个未命中并减少了我的生命计数。我需要的是关于如何在我的数组中搜索作为猜测输入的字母并返回它们在数组中的索引位置的一些提示,而不会认为数组中不是猜测的每个字符都是错误的输入。先感谢您。

package hangman;

import java.util.Scanner;

class Game {
int livesRemaining;
String letterGuessed;
String wordInput;
  char[] hiddenWord;
  char[] aOfWord ;

Scanner input = new Scanner(System.in);
boolean isFound;
int a;


public Game()
{
    this.setLives(8);
    //this.output();

    System.out.println("Player 1 please enter the word to be searched: ");
    wordInput = input.nextLine();

    aOfWord = wordInput.toCharArray();

    hiddenWord = new char[aOfWord.length];

    for(int j = 0; j < hiddenWord.length; j++)
        hiddenWord[j] = '*';

    this.output();

    while(livesRemaining > 0)
    {
        System.out.println("Please choose a letter: ");
        letterGuessed = input.nextLine();

        this.checkForMatch(letterGuessed);
        if(isFound == true)
    {
        hiddenWord[a] = letterGuessed.charAt(0);
    }
    else
    {
        System.out.println("Is not found!");
        this.reduceLives();
    }
    this.output();


  }

}

public void setLives(int a)
{
    this.livesRemaining = a;
}

public void reduceLives()
{
    livesRemaining = livesRemaining -1;
    System.out.println("Lives remaining: " + this.getLives());

}

public int getLives()
{
    return livesRemaining;
}

public void output()
{
    System.out.println("Lives remaining: " + this.getLives());
    System.out.println("Word found so far ");

    for(int i = 0; i < hiddenWord.length; i++)
    {
        System.out.print(hiddenWord[i] + "\n");
    }

}

public void checkForMatch(String l)
{

    for(int i = 0; i < aOfWord.length; i++)
        {
            //System.out.println("Comparing " + l.charAt(0) + " To " + aOfWord[i]);
            if(l.charAt(0) == aOfWord[i])   
            { 
               isFound = true;
               a = i;
               break;
            }
            else
            {
                isFound = false;  
            } 
        }

}

}

回答by Jeff LaJoie

To start with, if you want to return the indices of the chars, you'll need to add them somewhere, I would recommend returning an ArrayList which would hold all of your values. This is because an ArrayList can grow in size if it is too small and you'll not really need to worry about an out of bounds issue.

首先,如果你想返回字符的索引,你需要将它们添加到某个地方,我建议返回一个 ArrayList 来保存你的所有值。这是因为如果 ArrayList 太小,它的大小可能会增加,并且您真的不需要担心越界问题。

Your current form of checkForMatchworks for what you want, but consider returning a booleaninstead of setting your isFound field to true/false. Also there is a contains method that the String class has which you can call, so an alternative to your checkForMatch would be possible sort of like

您当前的checkForMatch工作形式boolean可以满足您的需求,但请考虑返回 a而不是将您的 isFound 字段设置为 true/false。还有一个你可以调用的 String 类的 contains 方法,所以你的 checkForMatch 的替代方法可能有点像

String yourString = "Hello";
String yourChar = "e";
System.out.println(yourString.contains(yourChar));

Which of course would print true!

这当然会打印为真!

Now, to get the indices, you already traverse the array and compare characters in your checkForMatch()method, why not simply create an ArrayList<Integer> matchedIndices = new ArrayList<Integer>();at the top of your method, and instead of setting isFound to true, call matchedIndices.add(i);if the characters match, and then at the end return the ArrayList?

现在,为了获取索引,您已经遍历数组并比较checkForMatch()方法中的字符,为什么不简单地ArrayList<Integer> matchedIndices = new ArrayList<Integer>();在方法顶部创建一个,而不是将 isFound 设置为 true,matchedIndices.add(i);如果字符匹配则调用,然后在最后调用返回ArrayList?

You would of course have to swap your return type from void to ArrayList, but there are many ways to go about this, this being just the first that came to my head!

您当然必须将返回类型从 void 交换为 ArrayList,但是有很多方法可以解决这个问题,这只是我想到的第一个!

回答by Sebastian_H

Your algorithm seems fine. However, it will only get you the last matching character because awill be rewritten whenever he finds a matching character. I think a really simple solution would be to do this in your checkForMatchmethod:

你的算法看起来不错。但是,它只会为您提供最后一个匹配的字符,因为a只要他找到匹配的字符就会被重写。我认为一个非常简单的解决方案是在您的checkForMatch方法中执行此操作:

if(l.charAt(0) == aOfWord[i])   
{ 
    isFound = true;
    hiddenWord[i] = l.charAt(0);
}

and also this in your gamemethod...

还有这在你的game方法中......

if(!isFound)
{
    System.out.println("Is not found!");
    this.reduceLives();
}

You don't have to use this.by the way. That is only necessary in certain cases. Take a look at this.

this.顺便说一下,您不必使用。这仅在某些情况下是必要的。看看这个