有点难以解释这里发生了什么。我正在用 java 制作刽子手以获得额外的学分任务。我想检查用户输入的字母是否在随机单词中。我有一个二维字符数组。0 列是答案的每个字母,1 列是正确猜测的字母。
char[] usedLetters =new char[guesses];
char[][] checker = new char[wordSize][2];
char[] answer = new char[wordSize];
String tempAnswer=word.randomWord();
System.out.println(tempAnswer);
answer=tempAnswer.toCharArray();
for(int i = 0;i<wordSize;i++)
{
checker[i][0]=answer[i];
}
System.out.println("Enter your first guess!");
char let=scan.next().charAt(0);
for(int i = 0; i<wordSize; i++)
{
if(checker[i][0]==let)
{
checker[i][1]=let;
System.out.println("found in" + i);
}
else
{
System.out.println("not found in" + i);
}
}
usedLetters[g]=let;
System.out.println("Guesses so far: " +g);
System.out.print("Letters used so far: ");
问题出现在第二个 for 循环中。没有 if else 语句正在打印到控制台。我无法解决这个问题。这是我在控制台中得到的:
有127142字可供选择
你想猜的单词多长?
9
你想猜几个?
10
丁酮类
输入您的第一个猜测!
b
到目前为止的猜测:1
目前使用的字母:
注意它是如何完全跳过 if else 输出的!我尝试了很多不同的方法,但它一直忽略我的匹配 for 循环。有什么建议么?
按照要求,这是 Hangman 课程的开始。对于任何有超过 6 个月编程经验的人来说,这看起来很可怕。
public class Hangman
{
private Scanner scan;
private int wordSize;
private Words word;
private int guesses;
private boolean winner;
public void setup()
{
scan = new Scanner(System.in);
word = new Words();
System.out.println("How long is the word you would like to guess?");
int wordSize=scan.nextInt();
word.setGameWordList(wordSize);
System.out.println("How many guesses would you like?");
guesses=scan.nextInt();
}
public void play()
{
int g = 1;
char[] usedLetters =new char[guesses];
char[][] checker = new char[wordSize][2];
char[] answer = new char[wordSize];
String tempAnswer=word.randomWord();
System.out.println(tempAnswer);
answer=tempAnswer.toCharArray();
for(int i = 0;i<wordSize;i++)
{
checker[i][0]=answer[i];
}
System.out.println("Enter your first guess!");
char let=scan.next().charAt(0);
for(int i = 0; i<wordSize; i++)
{
if(checker[i][0]==let)
{
checker[i][1]=let;
System.out.println("found in" + i);
}
else
{
System.out.println("not found in" + i);
}
}