猿问

使用 ArrayList<String> 将文本文件解析为字符串,用于问答结构化应用程序

这是我的第一篇文章,但我会直截了当。


我正在尝试用Java做一个测验。我的应用程序在当前状态下,从文本文件中读取问题,按换行符拆分问题,返回数据并使用扫描仪循环访问列表以检查输入以与存储的答案进行比较...好吧,还不是后一点。


一切都很好,但问题是我只有问题。我无法思考如何在文本文件中构建问题和答案,然后将其分解为某种可以在控制台中呈现给用户的数组结构。


所以,基本上,我想把问题,答案选择和正确答案都存储在一行上......或者,我可以以有效的方式完成所有这三件事。我假设我会用换行符来分隔,然后用其他一些字符来分隔我需要的每行的三个部分。我该从那里去哪里,如何?一旦我把它全部分解,我该如何跟踪哪个去哪里?


我对短篇小说的方法表示歉意。我到处寻找适合我需求的指南或教程。我也应该警告你...我对Java很陌生,我知道有更好的方法来做我需要的事情,所以,请......以一种新人可以理解的方式为我分解它。


我希望这一切都是有道理的。如果这在其他地方重复或太宽泛,请指导我去哪里,以便我可以学习。无论如何,这是我的代码!


问题数组.java

import java.util.ArrayList;

public class QuestionArray {

    private static ArrayList<String> list = new ArrayList<String>();

    public static Object[] processFile() {

        String file = FileStuff.fileAsString("res/questions/questions.txt");

        String[] lines = file.split("\n");

        for (int i = 0; i < lines.length; i++) {

            list.add(lines[i]);

        } return list.toArray();

    }

}

FileStuff.java

import java.io.BufferedReader;

import java.io.IOException;

import java.io.FileReader;

public class TextFileStuff {

    public static String fileAsString(String path) {

        StringBuilder builder = new StringBuilder();

        try {

            BufferedReader br = new BufferedReader(new FileReader(path));

            String line;

            while((line = br.readLine()) != null)

                builder.append(line + "\n");

            br.close();

        } catch (IOException e) {

            e.printStackTrace();

        } return builder.toString();

    }

}


ibeautiful
浏览 114回答 1
1回答

慕容3067478

首先,您需要确定文本文件的结构。这是你如何做到这一点。示例文本文件:3%%%@@@&nbsp;&&&What is Java?%%%a. Food@@@b. Programming Language@@@c. Person%%%bIs this a question?%%%a. Yes@@@b. No@@@c. Maybe%%%aAre you correct?%%%a. Yes@@@b. No@@@c. Maybe%%%c是文件中的问题数 是问题各部分的分隔符 是选项各部分的分隔符 是答案各部分的分隔符(如果存在具有多个答案的问题),后续部分是问题。格式为:3%%%@@@&&&(question part)%%%(choice part)%%%(answer part)由于有许多可能的选择,因此您可以像这样格式化可能的选项:这是来自上面的选项。(choice a)@@@(choice b)@@@(choice c)(choice part)由于可能有很多可能的答案,因此您可以像这样格式化可能的答案:这是来自上面的答案。(answer a)@@@(answer b)@@@(answer c)(answer part)一旦你决定了你的文本文件的外观,你可以创建一个类来保存这些问题,选择和答案。例如:public class Question {&nbsp; &nbsp; private String question; // This will hold the question&nbsp; &nbsp; private String[] choices;// This will hold the choices&nbsp; &nbsp; private String[] answers;// This will hold the correct answer/s, I made it an array since there might be&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // a question that has multiple answers&nbsp; &nbsp; public Question(String question, String[] choices, String[] answers) {&nbsp; &nbsp; &nbsp; &nbsp; this.question = question;&nbsp; &nbsp; &nbsp; &nbsp; this.choices = choices;&nbsp; &nbsp; &nbsp; &nbsp; this.answers = answers;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @return the question&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public String getQuestion() {&nbsp; &nbsp; &nbsp; &nbsp; return question;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param question&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the question to set&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public void setQuestion(String question) {&nbsp; &nbsp; &nbsp; &nbsp; this.question = question;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @return the choices&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public String[] getChoices() {&nbsp; &nbsp; &nbsp; &nbsp; return choices;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param choices&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the choices to set&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public void setChoices(String[] choices) {&nbsp; &nbsp; &nbsp; &nbsp; this.choices = choices;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @return the answer&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public String[] getAnswers() {&nbsp; &nbsp; &nbsp; &nbsp; return answers;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param answer&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the answer to set&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public void setAnswer(String[] answer) {&nbsp; &nbsp; &nbsp; &nbsp; this.answers = answer;&nbsp; &nbsp; }}现在你有一个班级,可以容纳这些问题,选择和答案。然后,您将创建一个读者类,该类将读取所有这些信息并将其传输到您的问题类。下面是一个示例:import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public class TextFileStuff {&nbsp; &nbsp; public static Question[] fileAsString(String path) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedReader br = new BufferedReader(new FileReader(path));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line = br.readLine(); // get the number of questions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Question[] questionList = new Question[Integer.parseInt(line)]; // initialize the question array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String partsDelimiter = br.readLine(); // get the delimiter for the question parts&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String choicesDelimiter = br.readLine(); // get the delimiter for the choices&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String answersDelimiter = br.readLine(); // get the delimiter for the answers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int questionNumber = 0; // initialize the question number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((line = br.readLine()) != null) { // loop through the file to get the questions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!line.trim().isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] questionParts = line.split(partsDelimiter); // split the line to get the parts of the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // question&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String question = questionParts[0]; // get the question&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] choices = questionParts[1].split(choicesDelimiter); // get the choices&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] answers = questionParts[2].split(answersDelimiter); // get the answers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Question q = new Question(question, choices, answers); // declare and initialize the question&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; questionList[questionNumber] = q; // add the question to the question list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; questionNumber++; // increment the question number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; br.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return questionList;&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }}现在您已经拥有了所有必需的部分,剩下的就是制作主要方法并将这些问题呈现给用户。你可以这样做:import java.util.Scanner;public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String path = "\\Test\\src\\res\\questions.txt"; // the path of the text file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // that contains the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // questions&nbsp; &nbsp; &nbsp; &nbsp; Question[] questionList = TextFileStuff.fileAsString(path);&nbsp; &nbsp; &nbsp; &nbsp; takeTheQuiz(questionList);&nbsp; &nbsp; }&nbsp; &nbsp; public static void takeTheQuiz(Question[] questions) {&nbsp; &nbsp; &nbsp; &nbsp; int score = 0;&nbsp; &nbsp; &nbsp; &nbsp; Scanner userInput = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < questions.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("======================");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Question " + (i + 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("======================");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(questions[i].getQuestion()); // Print the question&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] choices = questions[i].getChoices(); // get the choices&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < choices.length; j++) { // loop through the choices&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(choices[j]); // Print the choices&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Input an answer : "); // Print the choices&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String answer = userInput.nextLine().toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] answers = questions[i].getAnswers(); // get the answers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < answers.length; j++) { // loop through the answers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (answer.equals(answers[j])) { // check if the user's answer is correct&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score++; // increment score&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You scored " + score + " out of " + questions.length);&nbsp; &nbsp; &nbsp; &nbsp; if (score == questions.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Cheater!");&nbsp; &nbsp; &nbsp; &nbsp; } else if (score <= 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You suuuuck.");&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Mediocre performance.");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; userInput.close();&nbsp; &nbsp; }}以下是该程序的示例输出:======================Question 1======================What is Java?a. Foodb. Programming Languagec. PersonInput an answer : a======================Question 2======================Is this a question?a. Yesb. Noc. MaybeInput an answer : v======================Question 3======================Are you correct?a. Yesb. Noc. MaybeInput an answer : aYou scored 0 out of 3You suuuuck.现在你有一个有点动态的问答程序。如果要添加/删除问题,则只需修改文本文件即可。请随时发表评论以进行澄清和提出问题。
随时随地看视频慕课网APP

相关分类

Java
我要回答