我似乎有一個問題,我不能修復,當用戶輸入完整的單詞爲我的程序時,它顯示每次搜索字符,而不是隻顯示整個單詞說他們已經猜對了。我怎樣才能讓它只顯示單詞而不顯示每次搜索的時候用戶輸入整個單詞的字符?感謝未來的回信HangMan錯誤輸出
package assignment1Q2;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class HangmanClassExample {
static Scanner keyboard = new Scanner(System.in);
static int play, size, size2;
static String word;
static String[] ARRAY = new String[0];
public static void main(String[] args) {
setUpGame();
}
public static void setUpGame() {
System.err.printf("Welcome to hangman.\n");
try {
Scanner scFile = new Scanner(new File("H:\\Varsity work\\Java Programming\\Programs\\HangMan\\src\\hangman\\HangMan.txt"));
String line;
while (scFile.hasNext()) {
line = scFile.nextLine();
Scanner scLine = new Scanner(line);
size++;
}
ARRAY = new String[size];
Scanner scFile1 = new Scanner(new File("H:\\Varsity work\\Java Programming\\Programs\\HangMan\\src\\hangman\\HangMan.txt"));
while (scFile1.hasNext()) {
String word;
line = scFile1.nextLine();
Scanner scLine = new Scanner(line);
word = scLine.next();
ARRAY[size2] = word;
size2++;
calculateGuess();
}
} catch (FileNotFoundException e) {
System.out.println(e);
}
}
public static void calculateGuess() {
try {
do {
int random = (int) (Math.random() * ARRAY.length);
String randomWord = ARRAY[random];
String word = randomWord;
char[] ranWord = randomWord.toCharArray();
char[] dash = word.toCharArray();
int LEFT = 6;
for (int i = 0; i < dash.length; i++) {
dash[i] = '-';
System.out.print(dash[i]);
}
for (int A = 1; A <= dash.length;) {
System.out.print("\nGuess a Letter:");
String userletters = keyboard.next();;
for (int i = 0; i < userletters.length(); i++) {
char userLetter = userletters.charAt(i);
String T = Character.toString(userLetter);
for (int B = 0; B < ranWord.length; B++) {
if (userLetter == dash[B]) {
System.out.println("this '" + userLetter + "' letter already exist");
B++;
if (userLetter == dash[B-1]) {
break;
}
} else if (userLetter == ranWord[B]) {
dash[B] = userLetter;
A--;
}
}
if (!(new String(ranWord).contains(T))) {
LEFT--;
System.out.println("You did not guess a correct letter, you have " + LEFT + " OF "
+ dash.length + " trys left to guess correctly");
}
System.out.println(dash);
}
if ((new String(word)).equals(new String(dash))) {
System.out.println("\nYou have guessed the word correctly!");
break;
}
}
System.out.println("Play agian? (y/n)");
String name = keyboard.next();
if (name.equals("y")) {
play = 0;
} else {
play = 1;
return;
}
} while (play == 0);
} catch (NullPointerException e) {
}
}
}
OUTPUT:
Welcome to hangman.
--------
Guess a Letter:c
c-------
Guess a Letter:c
this 'c' letter already exist
c-------
Guess a Letter:computer
this 'c' letter already exist
c-------
co------
com-----
comp----
compu---
comput--
compute-
computer
You have guessed the word correctly!
Play agian? (y/n)
n
您應該閱讀[變量命名約定](http://www.oracle.com/technetwork/java/codeconventions-135099.html) – Blobonat
當您從播放器得到答案時,爲什麼不直接比較整個單詞與給定的一個,如果他們不匹配檢查的字母.. – 2016-09-29 13:30:36
@burhancerit我將如何做 –