很難解釋這裏發生了什麼。我正在讓java的hang子手做額外的功勞。我想檢查用戶輸入的字母是否在隨機字中。我有一個二維字符數組。 0欄是答案的每個字母,1欄是正確猜測的字母。匹配for循環中的兩個char類型數組,for循環不能正確執行
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: ");
問題出現在第二個循環中。其他if語句正在打印到控制檯。我無法解決這個問題。 以下是香港專業教育學院在控制檯得到:
有127142分的話從
選擇多久這個詞你想猜?
你會喜歡多少個猜測?
丁酮
輸入您的第一個猜測!
b
猜想迄今:1
快報沿用至今:它
注意如何完全如果兩者別的輸出跳過!我嘗試了很多不同的方式,但它一直忽略我的匹配循環。有什麼建議麼?
按照要求,這是Hang子手課程的開始。對於任何擁有超過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);
}
}
你確定'wordSize'不是0嗎? – irrelephant
是的,我在我的設置方法 – jeanqueq
在這種情況下,'wordSize'不會保留它的價值在你的猜測。也許你的意思是'word = new Words();的System.out.println( 「blablabla」); wordSize = scan.nextInt();' – irrelephant