您的猜测和程序会将结果与列表中的随机词进行比较。如果程序可以找到相同的符号,则会显示,如果不是,您将看到“#” - 符号。现在,我无法理解为什么“#”没有表现出色。这是完整的代码。
示例:
ap #############(15个音节,因为顾客不必知道 这个词的长度)
import java.util.Random; import java.util.Scanner;
public class Main {
private static Scanner scan = new Scanner(System.in);
public static final int N = 30;
public static void main(String[] args) {
String ranWord;
Random rand = new Random();
String[] words = new String[N];
words[0] = "appricot";
words[1] = "orange";
words[2] = "cucumber";
words[3] = "potato";
words[4] = "tomato";
words[5] = "cherry";
words[6] = "banana";
words[7] = "carrot";
words[8] = "were";
words[10] = "very";
words[11] = "tasty";
words[12] = "as";
words[13] = "usual";
words[14] = "and";
words[15] = "fresh";
words[16] = "and";
words[17] = "tasty";
words[18] = "passed";
words[19] = "for";
words[20] = "cooking";
words[21] = "a";
words[22] = "chicken";
words[23] = "it";
words[24] = "isn't";
words[25] = "necessary";
words[26] = "cook";
words[27] = "chicken";
words[28] = "every";
words[29] = "day";
System.out.println("Try to guess the word, call Your variant?" + "\n");
ranWord = words[rand.nextInt(N)];
System.out.println("Computer guess the word: " + ranWord);
Computer computer = new Computer(ranWord);
String customWord = scan.nextLine();
Customer customer = new Customer(customWord);
boolean finish = true;
while (!finish) {
//customWord = scan.nextLine();
if (customer.word.equals(computer.ranWord)) {
System.out.println("Succsesful prompt!");
finish = true;
} else {
checkIsFinish(customWord, ranWord);
finish = false;
}
}
}
static void checkIsFinish(String customWord, String ranWord) {
int minLenghtWord = customWord.length() < ranWord.length() ? customWord.length() : ranWord.length();
for (int i = 0; i < minLenghtWord; i++) {
if (customWord.charAt(i) == ranWord.charAt(i)) {
System.out.print(ranWord.charAt(i));
} else {
System.out.print("#");
}
}
for (int i = 0; i < 15 - minLenghtWord; i++) {
System.out.print("#");
}
System.out.println(customWord.length());
}
}
答案 0 :(得分:1)
这是一个愚蠢的错误。您永远不会输入while
因为finish = true
一开始。
这样做,
finish = true;
while (finish) {
//customWord = scan.nextLine();
if (customer.word.equals(computer.ranWord)) {
System.out.println("Succsesful prompt!");
} else {
checkIsFinish(customWord, ranWord);
}
finish = false;
}
或者,
finish = false;
while (!finish) {
//customWord = scan.nextLine();
if (customWord.equals(ranWord)) {
System.out.println("Succsesful prompt!");
} else {
checkIsFinish(customWord, ranWord);
}
finish = true;
}