因此,我正在尝试创建一个小型的子手迷你游戏。不幸的是,我一直收到同样的错误。我尝试过两次重做游戏,但似乎还是做不正确。我敢肯定这只是一些逻辑错误或什么。
我对Java还是比较陌生,因此将不胜感激。
我已将程序分为三类。一个用于Hangman对象,一个用于包含所述对象的数组,另一个用于使用这些对象的数组。下面的三个部分是游戏的摘录。
第1类:Hang子手
package hangman2;
import java.util.*;
import java.io.*;
public class Hangman {
private ArrayList<String> words = new ArrayList<>();
private int diff;
public Hangman(String fileName, int difficulty) {
diff = difficulty;
//easy = 0; medium = 1; hard = 2
try {
Scanner scanFile = new Scanner(new File(fileName));
while(scanFile.hasNext()) {
String line = scanFile.nextLine();
Scanner scanLine = new Scanner(line);
String tempWord = scanLine.next();
words.add(tempWord);
}
}
catch (FileNotFoundException e) {
System.out.println("File not found\n" + e);
}
}
//Gets
public ArrayList<String> getWords() { return words; }
public int getDifficulty() { return diff; }
//Sets
public void addWord(String word) { words.add(word); }
}
第2类:HangmanArray
package hangman2;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class HangmanArray {
private Hangman easyHangman;
private Hangman mediumHangman;
private Hangman hardHangman;
public HangmanArray() {
easyHangman = new Hangman("easy.txt", 0);
mediumHangman = new Hangman("medium.txt", 1);
hardHangman = new Hangman("hard.txt", 2);
}
public String getRandomWord() {
Hangman workingHangman = chooseDifficulty();
int max = workingHangman.getWords().size();
int randIndex = (int) (Math.random() * max);
return workingHangman.getWords().get(randIndex);
}
private Hangman chooseDifficulty() {
int chosenDifficulty = Integer.parseInt(JOptionPane.showInputDialog("Choose difficulty level\n"
+ "1: Novice\n2: Intermediate\n3: Expert"));
Hangman retHangman = null;
switch(chosenDifficulty) {
case 1: retHangman = easyHangman; break;
case 2: retHangman = mediumHangman; break;
case 3: retHangman = mediumHangman; break;
}
if (retHangman == null) {
System.out.println("Chosen difficulty not within range of [1;3]\nexiting");
System.exit(0);
}
return retHangman;
}
}
第3类:HangmanUI
package hangman2;
public class HangmanUI {
public static void main(String[] args) {
HangmanArray hangmanArray = new HangmanArray();
System.out.println(hangmanArray.getRandomWord());
}
}
选择难度2或3时,错误似乎来自HangmanArray的第25行:
return workingHangman.getWords().get(randIndex);
感谢您的帮助。
答案 0 :(得分:1)
您应该检查max
是否为0,如果是,则返回null
或空字符串,然后处理这种特殊情况。
我用Random.nextInt()代替了int randIndex = (int) (Math.random() * max);
从该随机数生成器的序列中返回一个伪随机数,该整数值在0(含)和指定值(不含)之间均匀分布。 nextInt的一般约定是伪随机生成并返回指定范围内的一个int值。所有绑定的可能的int值的产生概率(大约)相等
请记住,最后一个元素的索引为size()-1
public String getRandomWord() {
Hangman workingHangman = chooseDifficulty();
int max = workingHangman.getWords().size();
if (max == 0) {
return "";
}
int randIndex = new Random().nextInt(max); // returns an integer between 0 and max-1
return workingHangman.getWords().get(randIndex);
}
您还应该使用try-with-resources来确保正在使用的资源在关闭后被关闭。
public Hangman(String fileName, int difficulty) {
diff = difficulty;
//easy = 0; medium = 1; hard = 2
try (Scanner scanFile = new Scanner(new File(fileName))) {
while(scanFile.hasNext()) {
String line = scanFile.nextLine();
Scanner scanLine = new Scanner(line);
String tempWord = scanLine.next();
words.add(tempWord);
}
}
catch (FileNotFoundException e) {
System.out.println("File not found\n" + e);
}
}
最后,我建议使用debugger来了解您要放入每个Hang子手的内容。