第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 ArrayList<String> 將文本文件解析為字符串,用于問答結(jié)構(gòu)化應用程序

使用 ArrayList<String> 將文本文件解析為字符串,用于問答結(jié)構(gòu)化應用程序

ibeautiful 2022-08-03 12:51:30
這是我的第一篇文章,但我會直截了當。我正在嘗試用Java做一個測驗。我的應用程序在當前狀態(tài)下,從文本文件中讀取問題,按換行符拆分問題,返回數(shù)據(jù)并使用掃描儀循環(huán)訪問列表以檢查輸入以與存儲的答案進行比較...好吧,還不是后一點。一切都很好,但問題是我只有問題。我無法思考如何在文本文件中構(gòu)建問題和答案,然后將其分解為某種可以在控制臺中呈現(xiàn)給用戶的數(shù)組結(jié)構(gòu)。所以,基本上,我想把問題,答案選擇和正確答案都存儲在一行上......或者,我可以以有效的方式完成所有這三件事。我假設我會用換行符來分隔,然后用其他一些字符來分隔我需要的每行的三個部分。我該從那里去哪里,如何?一旦我把它全部分解,我該如何跟蹤哪個去哪里?我對短篇小說的方法表示歉意。我到處尋找適合我需求的指南或教程。我也應該警告你...我對Java很陌生,我知道有更好的方法來做我需要的事情,所以,請......以一種新人可以理解的方式為我分解它。我希望這一切都是有道理的。如果這在其他地方重復或太寬泛,請指導我去哪里,以便我可以學習。無論如何,這是我的代碼!問題數(shù)組.javaimport 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.javaimport 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();    }}
查看完整描述

1 回答

?
慕容3067478

TA貢獻1773條經(jīng)驗 獲得超3個贊

首先,您需要確定文本文件的結(jié)構(gòu)。這是你如何做到這一點。


示例文本文件:


3

%%%

@@@ 

&&&

What is Java?%%%a. Food@@@b. Programming Language@@@c. Person%%%b

Is this a question?%%%a. Yes@@@b. No@@@c. Maybe%%%a

Are you correct?%%%a. Yes@@@b. No@@@c. Maybe%%%c

是文件中

的問題數(shù) 是問題各部分的分隔符 是選項各

部分的分隔符 是答案

各部分的分隔符(如果存在具有多個答案的問題),

后續(xù)部分是問題。格式為:3%%%@@@&&&(question part)%%%(choice part)%%%(answer part)


由于有許多可能的選擇,因此您可以像這樣格式化可能的選項:這是來自上面的選項。(choice a)@@@(choice b)@@@(choice c)(choice part)


由于可能有很多可能的答案,因此您可以像這樣格式化可能的答案:這是來自上面的答案。(answer a)@@@(answer b)@@@(answer c)(answer part)


一旦你決定了你的文本文件的外觀,你可以創(chuàng)建一個類來保存這些問題,選擇和答案。例如:


public class Question {

    private String question; // This will hold the question

    private String[] choices;// This will hold the choices

    private String[] answers;// This will hold the correct answer/s, I made it an array since there might be

                                // a question that has multiple answers


    public Question(String question, String[] choices, String[] answers) {

        this.question = question;

        this.choices = choices;

        this.answers = answers;


    }


    /**

     * @return the question

     */

    public String getQuestion() {

        return question;

    }


    /**

     * @param question

     *            the question to set

     */

    public void setQuestion(String question) {

        this.question = question;

    }


    /**

     * @return the choices

     */

    public String[] getChoices() {

        return choices;

    }


    /**

     * @param choices

     *            the choices to set

     */

    public void setChoices(String[] choices) {

        this.choices = choices;

    }


    /**

     * @return the answer

     */

    public String[] getAnswers() {

        return answers;

    }


    /**

     * @param answer

     *            the answer to set

     */

    public void setAnswer(String[] answer) {

        this.answers = answer;

    }


}

現(xiàn)在你有一個班級,可以容納這些問題,選擇和答案。然后,您將創(chuàng)建一個讀者類,該類將讀取所有這些信息并將其傳輸?shù)侥膯栴}類。下面是一個示例:


import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;


public class TextFileStuff {


    public static Question[] fileAsString(String path) {

        try {

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

            String line = br.readLine(); // get the number of questions

            Question[] questionList = new Question[Integer.parseInt(line)]; // initialize the question array

            String partsDelimiter = br.readLine(); // get the delimiter for the question parts

            String choicesDelimiter = br.readLine(); // get the delimiter for the choices

            String answersDelimiter = br.readLine(); // get the delimiter for the answers

            int questionNumber = 0; // initialize the question number

            while ((line = br.readLine()) != null) { // loop through the file to get the questions

                if (!line.trim().isEmpty()) {

                    String[] questionParts = line.split(partsDelimiter); // split the line to get the parts of the

                                                                            // question

                    String question = questionParts[0]; // get the question

                    String[] choices = questionParts[1].split(choicesDelimiter); // get the choices

                    String[] answers = questionParts[2].split(answersDelimiter); // get the answers

                    Question q = new Question(question, choices, answers); // declare and initialize the question

                    questionList[questionNumber] = q; // add the question to the question list

                    questionNumber++; // increment the question number

                }

            }

            br.close();


            return questionList;

        } catch (IOException e) {

            e.printStackTrace();

        }

        return null;

    }

}

現(xiàn)在您已經(jīng)擁有了所有必需的部分,剩下的就是制作主要方法并將這些問題呈現(xiàn)給用戶。你可以這樣做:


import java.util.Scanner;


public class Main {

    public static void main(String[] args) {

        String path = "\\Test\\src\\res\\questions.txt"; // the path of the text file

                                                                                            // that contains the

                                                                                            // questions

        Question[] questionList = TextFileStuff.fileAsString(path);

        takeTheQuiz(questionList);

    }


    public static void takeTheQuiz(Question[] questions) {

        int score = 0;

        Scanner userInput = new Scanner(System.in);

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

            System.out.print("======================");

            System.out.print("Question " + (i + 1));

            System.out.println("======================");

            System.out.println(questions[i].getQuestion()); // Print the question

            String[] choices = questions[i].getChoices(); // get the choices

            for (int j = 0; j < choices.length; j++) { // loop through the choices

                System.out.println(choices[j]); // Print the choices

            }

            System.out.print("Input an answer : "); // Print the choices

            String answer = userInput.nextLine().toLowerCase();

            String[] answers = questions[i].getAnswers(); // get the answers

            for (int j = 0; j < answers.length; j++) { // loop through the answers

                if (answer.equals(answers[j])) { // check if the user's answer is correct

                    score++; // increment score

                }

            }

        }

        System.out.println("You scored " + score + " out of " + questions.length);

        if (score == questions.length) {

            System.out.println("Cheater!");

        } else if (score <= 2) {

            System.out.println("You suuuuck.");

        } else {

            System.out.println("Mediocre performance.");

        }

        userInput.close();

    }


}


以下是該程序的示例輸出:


======================Question 1======================

What is Java?

a. Food

b. Programming Language

c. Person

Input an answer : a

======================Question 2======================

Is this a question?

a. Yes

b. No

c. Maybe

Input an answer : v

======================Question 3======================

Are you correct?

a. Yes

b. No

c. Maybe

Input an answer : a

You scored 0 out of 3

You suuuuck.

現(xiàn)在你有一個有點動態(tài)的問答程序。如果要添加/刪除問題,則只需修改文本文件即可。請隨時發(fā)表評論以進行澄清和提出問題。


查看完整回答
反對 回復 2022-08-03
  • 1 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號