/*
Enter a letter in word: ******** > t
t is not in the word.
Enter a letter in word: ******** > a
Enter a letter in word: ******a* > l
Enter a letter in word: ******al > n
Enter a letter in word: *****nal > o
Enter a letter in word: ****onal > s
Enter a letter in word: ***sonal > a
a is already in the word.
Enter a letter in word: ***sonal > r
Enter a letter in word: **rsonal > e
Enter a letter in word: *ersonal > p
The word is: personal. You missed 2 times.
Do you want to guess for another word? Enter y or n> y
Enter a letter in word: *** > h
Enter a letter in word: *h* >
-------------------------------------------------------
Enter a letter in word: ** > o
o is not in the word.
Enter a letter in word: ** > i
Enter a letter in word: i* > t
The word is: it. You missed 1 time.
Do you want to guess for another word? Enter y or n> i
Invalid value.
Do you want to guess for another word? Enter y or n> n
Done.
*/
import java.util.Scanner;
import java.util.Random;
public class GuessWord {
public static void main(String[] args) {
String[] words = {"many", " experts", "call", "it", "the", "best",
"personal", "computer", "operating", "system", "on", "earth"};
int key;
Random random = new Random();
Scanner input = new Scanner(System.in);
for (int j = 0; j < words.length; j++) {
key = random.nextInt(11); // 生成0~11的随机整数
System.out.print("Enter a letter in word: ");
char[] guess = initialize(words[key]);
int missedTimes = guess(guess, words[key]);
System.out.println("The word is: " + words[key] + ". You missed " + missedTimes
+ ((missedTimes > 1) ? " times." : " time."));
while (j < words.length) {
System.out.print("Do you want to guess for another word? Enter y or n> ");
String s = input.next();
if (s.charAt(0) == 'n') {
j = words.length;
System.out.println("Done.");
}
else if (s.charAt(0) == 'y')
break;
else
System.out.println("Invalid value.");
}
}
}
public static char[] initialize(String word) {
/* 创建char数组guess并将其全部初始化为'*',不建议使用字符串,使用字符串
* 处理的话会很麻烦。
*/
char[] guess = new char[word.length()];
for (int i = 0; i < word.length(); i++) {
guess[i] = '*';
System.out.print(guess[i]);
}
System.out.print(" > ");
return guess;
}
public static int guess(char[] g, String w) {
Scanner input = new Scanner(System.in);
int i, missedTimes = 0, j = 0;
while (j < w.length()) {
String s = input.next();
boolean equalWS = true;
for (i = 0; i < w.length(); i++)
if (w.charAt(i) == s.charAt(0) && g[i] == s.charAt(0)) {
System.out.println(s.charAt(0) + " is already in the word.");
missedTimes++;
break;
}
if (i == w.length()) {
for (i = 0; i < w.length(); i++) {
if (w.charAt(i) == s.charAt(0)) {
g[i] = s.charAt(0);
j++;
equalWS = false;
} else if (equalWS && (i == w.length() - 1)) {
System.out.println(s.charAt(0) + " is not in the word.");
missedTimes++;
}
}
}
if (j < w.length()) // 如果去掉if (j < w.length()),while循环会多执行一次
continueGuess(g);
}
return missedTimes;
}
public static void continueGuess(char[] g) {
System.out.print("Enter a letter in word: ");
for (int i = 0; i < g.length; i++)
System.out.print(g[i]);
System.out.print(" > ");
}
}
Introduction to Java Programming编程题9.31<刽子手游戏>
最新推荐文章于 2022-06-11 21:14:22 发布