【发布时间】:2014-11-03 01:13:18
【问题描述】:
除了用特征替换隐藏词的“_ _ _ ”之外,我几乎知道如何做这个项目,例如:“ o o _”。如何用字符串替换特征并输出数据。忽略注释,它们只是对代码的一些随机想法。这是我目前所拥有的:
import java.util.Random;
import java.util.Scanner;
public class ProjectNum2 {
// Alex Berencz
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
String letterguess;
String wordguess;
String hangmanwords = "";
char letter;
String con = "yes";
String quit = "no";
int computer = 0;
int player = 0;
boolean done = false;
int guess = 6;
// while(!done){
System.out.println("Welcome to the Hangman Word game!");
String[] hangmanWords = { "apple", "triangle", "assassin", "mercenary",
"loop", "snake", "lion", "algorithm", "turtle", "return",
"message", "heart", "halloween", "door", "fruit", "band",
"married", "summer", "choice", "elephant" };
String[] spaces = { "_ _ _ _ _", "_ _ _ _ _ _ _ _", "_ _ _ _ _ _ _",
"_ _ _ _ _ _ _ _ _", "_ _ _ _", "_ _ _ _ _", "_ _ _ _",
"_ _ _ _ _ _ _ _ _", "_ _ _ _ _ _", "_ _ _ _ _ _",
"_ _ _ _ _ _ _", "_ _ _ _ _", "_ _ _ _ _ _ _ _ _", "_ _ _ _",
"_ _ _ _ _", "_ _ _ _", "_ _ _ _ _ _ _", "_ _ _ _ _ _",
"_ _ _ _ _ _", "_ _ _ _ _ _ _ _" };
int index = (int) RandomWord(hangmanWords, spaces, hangmanwords);
System.out.println();
System.out.println("Choose a letter or enter zero to guess the word: ");
letterguess = kbd.next();
String guesscomplete = "0";
String fixedkey = letterguess.toLowerCase();
char[] charkey = new char[letterguess.length()];
// for(int x=0; x>letterguess.charAt(x); x++){
// charkey[x]=letterguess.charAt(x);
if (letterguess.equals(guesscomplete)) {
System.out.println("Guess the word! ");
wordguess = kbd.next();
if (wordguess.equals(hangmanWords[index])) {
System.out.println("That is correct!");
player++;
System.out.println("Would you like to play again?");
String again = kbd.next();
if (again.equals(quit)) {
System.out.println("Computer wins: " + computer
+ " Player wins: " + player);
System.out.println("See you later!");
}
else {
System.out.println("That is not correct!");
System.out.println("Would you like to play again?");
again = kbd.next();
computer++;
}
}
}
while (hangmanWords[index].contains(letterguess)) {
System.out.println(hangmanWords[index].replace(hangmanWords[index],
letterguess));
System.out
.println("Choose a letter or enter zero to guess the word: ");
letterguess = kbd.next();
}
if (!hangmanWords[index].contains(letterguess)) {
guess--;
System.out.println("The letter is not in the word. You have "
+ guess + " more guesses.");
System.out
.println("Choose a letter or enter zero to guess the word: ");
letterguess = kbd.next();
if (guess == 0) {
computer++;
}
}
}
// }
// }
private static Object RandomWord(String[] hangmanWords, String[] spaces,
String hangmanwords) {
Random ranIndex = new Random();
int index = ranIndex.nextInt(hangmanWords.length);
hangmanwords = spaces[index];
System.out.print(hangmanwords);
return (index);
}
}
这是我的输出:
Welcome to the Hangman Word game!
_ _ _ _ _ _ _
Choose a letter or enter zero to guess the word:
a
a
Choose a letter or enter zero to guess the word:
【问题讨论】:
-
您需要使用
StringBuilder和循环。循环遍历“秘密”单词,并为每个匹配用户猜测的字符,将输出中的字符替换为相应的字符...现在,如果您巧妙地执行此操作,您只需执行一次即可进行猜测跨度> -
您还可以使用正则表达式 (vogella.com/tutorials/JavaRegularExpressions/…) 隐藏答案中尚未找到的所有字符。
-
我不得不说这个标题真的很糟糕。它无助于任何人对你的问题应该问什么有高层次的理解。