我正在处理一个典型的hangman程序,我在命令参数的txt文件中有几个单词。我的项目文件中还有另一个带有随机单词的txt文件。
错误/问题#1
我试图找出当有人输入游戏时如何让游戏停止。当我提示用户看他们是否想再次播放时。 我想我需要插入一个
complete = true;
某个地方,但不知道在哪里!
(Guess) Enter a letter in word ***> c
(Guess) Enter a letter in word c**> a
(Guess) Enter a letter in word ca*> r
The word is car , you missed 0 time(s)
Do you want to guess another word? Enter y or n > n
(Guess) Enter a letter in word car>
错误#2:当我运行我的代码时,它运行的时间减少了一半,另一半则显示了此错误消息
Exception in thread "main" java.lang.NullPointerException
at Hangman.asteriskCover(Hangman.java:110)
at Hangman.main(Hangman.java:63)
Java Result: 1
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author joe
*/
public class Hangman {
public static void main(String[] args) throws IOException {
String fileName;
Scanner reader;
Scanner typist;
Random generator = new Random();
boolean completed;
StringBuffer asterisk;
String guess;
String guesses;
char again = 'n';
char lett;
int timesMissed;
if (args.length == 0) {
fileName = "worden.txt";
} else {
fileName = args[0];
}
//using filename to create a new file object
java.io.File file = new java.io.File(fileName);
if (!file.exists()) {
System.err.println("Error: File " + fileName + " does not exist.");
System.exit(1);
}
reader = new Scanner(file);
String[] wordArray = new String[15];
String word;
int i=0;
while (reader.hasNext()) {
word = reader.next();
wordArray[i] = word;
i++;
}
typist = new Scanner(System.in);
String worden = wordArray[(int) (Math.random() * wordArray.length)];
do {
completed = false;
guesses = "";
timesMissed = 0;
//create the asterisks
asterisk = asteriskCover(worden);
while (!completed) {
System.out.print("(Guess) Enter a letter in word " + asterisk +
"> " );
guess = typist.next();
//if you're weird and type out the whole thing
if (guess.length() > 1) {
if (guess.equals(worden)) {
System.out.println("Holy #$@%!, good job!");
} else {
System.out.println("Nope, that's not it.");
}
completed = true;
//if you're not weird and just guess a letter like I
// had originally asked you to.
} else {
lett = guess.charAt(0);
guesses += lett;
//2 lines below rep if letter isn't there and increments
//times missed. (hopefully)
if (worden.indexOf(lett) < 0) {
timesMissed++;
} else {
//my awesome method to put the asterisk in
rightLett(worden, asterisk, lett);
}
if (worden.equals(asterisk.toString())){
System.out.println("The word is " + worden + " , you missed "
+ timesMissed + " time(s)");
System.out.print("Do you want to guess another word?"
+ " Enter y or n > ");
again = typist.next().charAt(0);
}
}
}
}
while(again == 'Y' || again == 'y');
reader.close();
}
public static StringBuffer asteriskCover(String a) {
StringBuffer asterisk = new StringBuffer(a.length());
for (int count = 0; count < a.length(); count++) {
asterisk.append('*');
}
return asterisk;
}
public static void rightLett(String worden, StringBuffer asterisk, char lett) {
for (int dex = 0; dex < worden.length(); dex++) {
if (worden.charAt(dex) == lett) {
asterisk.setCharAt(dex, lett);
}
}
}
}
答案 0 :(得分:2)
问题似乎是你的停止条件:while (again == 'Y' || again == 'y');
在内循环之外:while(!completed)
。
要解决这个问题,你可以这样做:
again = typist.next().charAt(0);
completed = true; //Add this line
上面应该打破内部循环(while(!completed)
),执行将进入外部循环(do...while(again == 'Y' || again == 'y');
)。
将此行:String worden = wordArray[(int) (Math.random() * wordArray.length)];
转到此处:
do {
String worden = wordArray[(int) (Math.random() * wordArray.length)];
...
由于您是从外部循环外部设置的,因此未更新该单词。那应该解决它。
答案 1 :(得分:0)
如果你添加这个代码部分循环结束,那就没问题了。
if(again == 'N' || again == 'n') break;