另一个 Class 中的方法不返回任何内容
Method in another Class not returning anything
我正在从事一个创建 Hangman 游戏的项目。 GameManager Class 应该从 AnswerBank Class 调用 getRandomAnswer 方法,这应该 return 来自名为 noun_noun.txt 的文件中的两个名词的随机答案(我已经放置了这个文件在项目文件夹的第一层)。但是,每当我尝试测试代码时,在出现提示时键入 "yes" 后,控制台不会产生任何结果。谁能帮忙?
public class GameManager {
private ArrayList<String> puzzleHistory;
private int numPuzzlesSolved;
private AnswerBank bank;
public GameManager(String fn) throws IOException{
numPuzzlesSolved = 0;
puzzleHistory = new ArrayList<String>();
this.bank = new AnswerBank(fn);
}
public static void main(String[] args) throws IOException {
GameManager obj = new GameManager("noun_noun.txt");
obj.run();
}
public void run() throws FileNotFoundException{
Scanner scan = new Scanner(System.in);
System.out.println("Would you like to play Hangman? Please type 'yes' or 'no'.");
String inputAns = scan.next();
if(inputAns.equalsIgnoreCase("no")){
System.exit(0);
}
else if(inputAns.equalsIgnoreCase("yes")){
System.out.println(bank.getRandomAnswer());
}
else{
System.out.println("Invalid response!");
}
scan.close();
}
}
public class AnswerBank {
private ArrayList<String> listStrings;
private File gameFile;
public AnswerBank(String fileName) throws IOException{
File gameFile = new File(fileName);
this.gameFile = gameFile;
this.listStrings = new ArrayList<String>();
}
public String getRandomAnswer() throws FileNotFoundException{
Scanner fileScan = new Scanner(gameFile);
int totalLines = 0;
while(fileScan.hasNextLine()){
totalLines++;
}
for(int i = 0; i < totalLines; i++){
this.listStrings.add(fileScan.nextLine());
}
int randInt = (int)(Math.floor ( Math.random() * totalLines));
String randAns = listStrings.get(randInt);
fileScan.close();
return randAns;
}
}
后面会用到puzzleHistory和numPuzzlesSolved,请无视。在此先感谢您的帮助。
问题出在这里:
while (fileScan.hasNextLine()) {
totalLines++;
}
你的文件扫描器永远不会移动文件中的位置,所以它会一直扫描第一行,因为第一行不为空,所以这是一个无限循环。
此处的一个简单解决方法是在阅读单词的同时进行计数:
public String getRandomAnswer() throws FileNotFoundException {
Scanner fileScan = new Scanner(gameFile);
int totalLines = 0;
while (fileScan.hasNext()) {
totalLines++;
this.listStrings.add(fileScan.nextLine());
}
int randInt = (int) (Math.floor(Math.random() * totalLines));
String randAns = listStrings.get(randInt);
fileScan.close();
return randAns;
}
希望对您有所帮助。
我正在从事一个创建 Hangman 游戏的项目。 GameManager Class 应该从 AnswerBank Class 调用 getRandomAnswer 方法,这应该 return 来自名为 noun_noun.txt 的文件中的两个名词的随机答案(我已经放置了这个文件在项目文件夹的第一层)。但是,每当我尝试测试代码时,在出现提示时键入 "yes" 后,控制台不会产生任何结果。谁能帮忙?
public class GameManager {
private ArrayList<String> puzzleHistory;
private int numPuzzlesSolved;
private AnswerBank bank;
public GameManager(String fn) throws IOException{
numPuzzlesSolved = 0;
puzzleHistory = new ArrayList<String>();
this.bank = new AnswerBank(fn);
}
public static void main(String[] args) throws IOException {
GameManager obj = new GameManager("noun_noun.txt");
obj.run();
}
public void run() throws FileNotFoundException{
Scanner scan = new Scanner(System.in);
System.out.println("Would you like to play Hangman? Please type 'yes' or 'no'.");
String inputAns = scan.next();
if(inputAns.equalsIgnoreCase("no")){
System.exit(0);
}
else if(inputAns.equalsIgnoreCase("yes")){
System.out.println(bank.getRandomAnswer());
}
else{
System.out.println("Invalid response!");
}
scan.close();
}
}
public class AnswerBank {
private ArrayList<String> listStrings;
private File gameFile;
public AnswerBank(String fileName) throws IOException{
File gameFile = new File(fileName);
this.gameFile = gameFile;
this.listStrings = new ArrayList<String>();
}
public String getRandomAnswer() throws FileNotFoundException{
Scanner fileScan = new Scanner(gameFile);
int totalLines = 0;
while(fileScan.hasNextLine()){
totalLines++;
}
for(int i = 0; i < totalLines; i++){
this.listStrings.add(fileScan.nextLine());
}
int randInt = (int)(Math.floor ( Math.random() * totalLines));
String randAns = listStrings.get(randInt);
fileScan.close();
return randAns;
}
}
后面会用到puzzleHistory和numPuzzlesSolved,请无视。在此先感谢您的帮助。
问题出在这里:
while (fileScan.hasNextLine()) {
totalLines++;
}
你的文件扫描器永远不会移动文件中的位置,所以它会一直扫描第一行,因为第一行不为空,所以这是一个无限循环。
此处的一个简单解决方法是在阅读单词的同时进行计数:
public String getRandomAnswer() throws FileNotFoundException {
Scanner fileScan = new Scanner(gameFile);
int totalLines = 0;
while (fileScan.hasNext()) {
totalLines++;
this.listStrings.add(fileScan.nextLine());
}
int randInt = (int) (Math.floor(Math.random() * totalLines));
String randAns = listStrings.get(randInt);
fileScan.close();
return randAns;
}
希望对您有所帮助。