0

我正在为学校做这个项目,我真的被困在这一点上。基本上我必须拿一个文本文件。然后我必须用其中的信息填充一个数组列表。之后,我需要正确格式化它,以便我可以将它用于类别、问题和答案。我不知道该怎么做,所以下面是教练问我们这是否有助于进一步清除它。

“在这个作业中,你将编写一个模拟琐事游戏的应用程序。你将获得一个文本文件,其中包含六个不同类别的一组问题和答案。你的程序将从每个类别中随机选择一个问题来回答玩家。每次运行结束时会显示得分。

输入数据格式:输入文件包含不同类别的问题和答案。对于每个类别,第一行表示类别的名称。这条线后面会跟着多对线。该对的第一行是问题,第二行是其对应的答案。一个空行分隔类别。技术要求:

 创建一个名为 TriviaQuestion 的类,它代表一个典型的问题对象。

 读取数据后,将信息存储在 ArrayList(s) 中。

 在您的输入/输出中保持用户友好。忽略案例。如果可能,请接受部分正确的答案。”

现在,正如我的代码将显示的那样,我被困在如何以他指定的格式将信息添加到 ArrayList 中。我不希望任何人为我做我的工作。我只需要朝正确的方向轻推。我不知道每个类别是否需要许多数组列表,如果需要,是否需要将它们放在单独的类中?这只是我的第二个学期,所以我仍然对这些事情感到困惑。在网上和这个论坛上还有其他 jave 琐事游戏,但我找不到任何以这种方式工作的游戏。无论如何,这是代码和之后的文本文件......

import java.io.File;
import java.util.ArrayList;


public class TriviaGame {

/**
 * I want to make several array lists and then use a random question for each         category. Then compare the user
 * answer to the real answer to see if they won. Keep a count of the correct answers. Ignore case. 
 */
//instance fields
private String player; // The player
private int points;        // Player's number of points
private String currentAnswer; // Current typed answer
private File gameFile = new File ("trivia.txt");


//constructors

public TriviaGame(String playerName)
{
    playerName = player;
    points = 0;
}


public TriviaGame(String player, int points, String currentAnswer,
        File gameFile, ArrayList<String> triviaQuestion) {
    super();
    this.player = player;
    this.points = points;
    this.currentAnswer = currentAnswer;
    this.gameFile = gameFile;
    }


//Getters and Setters
/**
 * @return the player
 */
public String getPlayer() {
    return player;
}


/**
 * @param player the player to set
 */
public void setPlayer(String player) {
    this.player = player;
}


/**
 * @return the points
 */
public int getPoints() {
    return points;
}


/**
 * @param points the points to set
 */
public void setPoints(int points) {
    this.points = points;
}


/**
 * @return the currentAnswer
 */
public String getCurrentAnswer() {
    return currentAnswer;
}


/**
 * @param currentAnswer the currentAnswer to set
 */
public void setCurrentAnswer(String currentAnswer) {
    this.currentAnswer = currentAnswer;
}


/**
 * @return the gameFile
 */
public File getGameFile() {
    return gameFile;
}


/**
 * @param gameFile the gameFile to set
 */
public void setGameFile(File gameFile) {
    this.gameFile = gameFile;
}


//To String Method

/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
    return "TriviaGame [player=" + player + ", points=" + points
            + ", currentAnswer=" + currentAnswer + ", gameFile=" +     gameFile
            + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
            + ", toString()=" + super.toString() + "]";
}




}

紧随其后的是测试人员......

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;



public class TriviaGamePlayer {

/**
 * @param args
 */
public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    
    File gameFile = new File ("trivia.txt");
    ArrayList<String> triviaQuestion = new ArrayList<String>();
    
    Scanner infile = new Scanner(gameFile);
    String line;
    

    while(infile.hasNext()){
        line = infile.nextLine();
        triviaQuestion = line.split();

}

}


}

最后是一些伴随它的文本文件......

Arts & Literature
What are bongo drums traditionally held between for playing?
The knees
The Hugo Awards are given for the best literature in which genre?
Science fiction
What four-letter girl's name gave Jane Austen the title of a comic novel?
Emma
Andy Warhol was born in what US city?
Pittsburgh
Who wrote the novel "The Chocolate War"?
Robert Cormier
What surname for John in "The Importance of Being Earnest" did Oscar Wilde take from the seaside town he vacationed at?
Worthing

Geography
What is the modern day equivalent of Dacia?
Romania
What is the capital of Ontario?
Toronto
Which island boasts of being Napoleon's birthplace?
Corsica
What nationality is a Breton?
French
A person from North Carolina is properly known as a what?
North Carolinian
In which country would you find St. Basil's cathedral?
Russia
4

1 回答 1

0

您需要一个 ArrayList 或 TriviaQuestion 对象

List<TriviaQuestion> triviaQuestions = new ArrayList<TriviaQuestion>();

像这样以最简单的形式创建一个 TriviaQuestion 类

public class TriviaQuestion {
  String category;
  String question;
  String answer;
}

你可以像这样填充它

public static void main(String[] args) throws IOException {
    File gameFile = new File("trivia.txt");
    List<TriviaQuestion> triviaQuestions = new ArrayList<TriviaQuestion>();
    Scanner infile = new Scanner(gameFile);
    String lastKnownCategory = "";

    while (infile.hasNextLine()) {
        String currentLine = infile.nextLine();

        if (!currentLine.isEmpty()) {
            TriviaQuestion currentQuestion = new TriviaQuestion();

            if (currentLine.endsWith("?")) {
                currentQuestion.category = lastKnownCategory;
                currentQuestion.question = currentLine;
                currentQuestion.answer = infile.nextLine();
            } else {
                currentQuestion.category = currentLine;
                currentQuestion.question = infile.nextLine();
                currentQuestion.answer = infile.nextLine();
                lastKnownCategory = currentLine;
            }
            triviaQuestions.add(currentQuestion);
        }
    }
}
于 2013-09-20T03:19:42.563 回答