Java 爪哇家庭作业刽子手

Java 爪哇家庭作业刽子手,java,Java,因此,对于一个类,我们必须制作一个刽子手游戏,它可以让用户输入一个单词,然后让另一个人解决它。它必须能够识别单词中的多个重复字母,否则我就完了。下面是我的代码,在我删除checkformatch方法中的break语句之前,它工作得非常好,这样它就超过了字母的初始查找。有了中断,它就再也找不到第二个、第三个、第三个、第三个、第三个、第三个、第二个、第三个、第三个、第三个、第三个、第三个、第三个、第三个、第三个、第三个、第三个、第三个、第三个、第三个。我需要的是一些提示,告诉我如何在数组中搜索作为猜

因此,对于一个类,我们必须制作一个刽子手游戏,它可以让用户输入一个单词,然后让另一个人解决它。它必须能够识别单词中的多个重复字母,否则我就完了。下面是我的代码,在我删除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;  
            } 
        }

}

}
packagehangman;
导入java.util.Scanner;
班级游戏{
int-livesreing;
猜字符串字母;
字符串输入;
char[]隐藏词;
char[]aOfWord;
扫描仪输入=新扫描仪(System.in);
布尔型;
INTA;
公共游戏()
{
这是我的生命(8);
//这个是.output();
System.out.println(“玩家1请输入要搜索的单词:”);
wordInput=input.nextLine();
aOfWord=wordInput.toCharArray();
hiddenWord=新字符[aOfWord.length];
for(int j=0;j0)
{
System.out.println(“请选择一个字母:”);
letterGuessed=input.nextLine();
此。检查格式(字母猜测);
if(isFound==true)
{
hiddenWord[a]=猜出的字母。字符(0);
}
其他的
{
System.out.println(“未找到!”);
这个。reduceLives();
}
这个是.output();
}
}
公共生活(INTA)
{
this.livesRemaining=a;
}
公共void reduceLives()
{
livesRemaining=livesRemaining-1;
System.out.println(“剩余生命:+this.getlifes());
}
公共生活
{
回归生活;
}
公共无效输出()
{
System.out.println(“剩余生命:+this.getlifes());
System.out.println(“迄今为止找到的单词”);
for(int i=0;i
您的算法似乎很好。但是,它只会为您获取最后一个匹配字符,因为只要找到匹配字符,
a
就会被重写。 我认为一个非常简单的解决方案是在
checkForMatch
方法中执行此操作:

if(l.charAt(0) == aOfWord[i])   
{ 
    isFound = true;
    hiddenWord[i] = l.charAt(0);
}
在你的
游戏中
方法也是如此

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

您不必使用
这个。
顺便说一句。只有在某些情况下才有必要这样做。看看。

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

if(l.charAt(0) == aOfWord[i])   
{ 
    isFound = true;
    hiddenWord[i] = l.charAt(0);
}
在你的
游戏中
方法也是如此

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

您不必使用
这个。
顺便说一句。只有在某些情况下才有必要这样做。看看。

首先,如果要返回字符的索引,需要将它们添加到某个位置,我建议返回一个保存所有值的ArrayList。这是因为如果ArrayList太小,它的大小可能会增加,因此您不必担心超出范围的问题

您当前的“代码”> CuffFaltCG/<代码>为您所需要的工作,但是考虑返回<代码>布尔,而不是将ISSIDE字段设置为true / false。另外,String类还有一个contains方法,您可以调用它,因此可以使用类似于checkForMatch的替代方法

String yourString = "Hello";
String yourChar = "e";
System.out.println(yourString.contains(yourChar));
这当然是真的

现在,为了获得索引,您已经遍历了数组并比较了
checkForMatch()
方法中的字符,为什么不简单地创建一个
ArrayList matchedIndices=new ArrayList()matchedinces.add(i),而不是将isFound设置为true如果字符匹配,然后在末尾返回ArrayList


当然,您必须将返回类型从void切换到ArrayList,但是有很多方法可以实现这一点,这只是我第一次想到的

首先,如果要返回字符的索引,需要将它们添加到某个位置,我建议返回一个保存所有值的ArrayList。这是因为如果ArrayList太小,它的大小可能会增加,因此您不必担心超出范围的问题

您当前的“代码”> CuffFaltCG/<代码>为您所需要的工作,但是考虑返回<代码>布尔,而不是将ISSIDE字段设置为true / false。另外,String类还有一个contains方法,您可以调用它,因此可以使用类似于checkForMatch的替代方法

String yourString = "Hello";
String yourChar = "e";
System.out.println(yourString.contains(yourChar));
这当然是真的

现在,为了获得索引,您已经遍历了数组并比较了
checkForMatch()
方法中的字符,为什么不简单地创建一个
ArrayList matchedIndices=new ArrayList()matchedinces.add(i),而不是将isFound设置为true如果字符匹配,然后在末尾返回ArrayList

当然,您必须将返回类型从void交换到ArrayList,bu