私のプログラムでchar配列を出力しようとしていますが、私のコンソールでは、配列は5つのボックスとして表示されます。文字列を出力すると空のボックスが出力されますか?
私のコードは、文字をとり、inputArray(char)をスキャンして一致するものを探し出すゲームです。一致するものがあれば、正しく推測された文字をcurrentGuessArrayの対応する位置に追加します。配列を正しく設定していませんか?
for (int i = 0; i == lengthOfWord; i++) {
if (guess.charAt(0) == inputArray[i]) {
currentGuessArray[i] = guess.charAt(0);
}
}
System.out.println(currentGuessArray);
これは私が現在
を出力しています私の完全なコードは、あなたがString
表現でchar
配列を印刷したい場合は、あなたが作成する必要が
public class Console {
static String input = "";
public static String get() {
@SuppressWarnings("resource")
System.out.println("Enter the word to guess");
Scanner s = new Scanner(System.in);
input = s.nextLine();
return input;
}
}
public class WordGuess {
public static void guess() {
Scanner s = new Scanner(System.in);
String guess;
int trys = 0;
String input = ConsoleView.input;
int length = input.length();
char[] inputArray = input.toCharArray();
boolean[] currentGuess = new boolean[length];
char[] currentGuessArray = new char[length];
while (currentGuessArray != inputArray) {
System.out.println("Key in one character or your guess word:");
trys++;
guess = s.nextLine();
int guessLength = guess.length();
if (guessLength == 1) {
for (int i = 0; i == length; i++) {
if (guess.charAt(0) == inputArray[i]) {
//currentGuess[i] = true;
currentGuessArray[i] = guess.charAt(0);
}
}
System.out.println(Arrays.toString(currentGuessArray));
} else if (guess.equals(input)) {
System.out.println("You're correct!");
break;
}
else if (currentGuessArray == inputArray) {
System.out.println("Congratulations, you got the word in " + trys);
}
}
}
}
ハァッ、うわー、私はそのような小さな間違いを見落としたとは信じられない!ありがとう、これは修正されました! – snazzyjazzy
甘い!私は助けることができてうれしいです。私の答えを受け入れたものとしてマークすることを忘れないでください。 – tyrel