Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从具有特定结构的txt文件中获取试题?_Java_Substring_Bufferedreader_Stringbuilder_Filereader - Fatal编程技术网

Java 如何从具有特定结构的txt文件中获取试题?

Java 如何从具有特定结构的txt文件中获取试题?,java,substring,bufferedreader,stringbuilder,filereader,Java,Substring,Bufferedreader,Stringbuilder,Filereader,我想做一个程序,从特定结构的文件中获取问题和答案,并让用户给出答案。程序还必须计算正确答案并向用户显示 以下是文本文件结构的示例: What year is it right now? 2 1) 1900 2) 2014 3) 3200 --- Which is the biggest country in the world? 1 1) Russia 2) United States of America 3) United Kingdom --- 这是我写的代码,但有点不对劲,我看不出到底

我想做一个程序,从特定结构的文件中获取问题和答案,并让用户给出答案。程序还必须计算正确答案并向用户显示

以下是文本文件结构的示例:

What year is it right now?
2
1) 1900
2) 2014
3) 3200
---
Which is the biggest country in the world?
1
1) Russia
2) United States of America
3) United Kingdom
---
这是我写的代码,但有点不对劲,我看不出到底是什么:

public class testLoader {

private static BufferedReader br;
private static int answerCounter;

public static void main(String[] args) {

    try {
        br = new BufferedReader(new FileReader("D:/test.txt"));
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            String answer=null;
            if(line.startsWith("*")){
                answer = line;
            }
            while (line != "---") {
                line = br.readLine();
                sb.append(line);
                sb.append(System.lineSeparator());
            }
            System.out.println(sb.toString());
            answerCheck(answer);

            line = br.readLine();
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    System.out.println("You have " + answerCounter + "correct answers");
}

public static void answerCheck(String rightAnswer) {
    System.out.println("What's your answer?");
    Scanner input = new Scanner(System.in);
    String answer = input.nextLine();
    answerCounter = 0;
    if (answer == rightAnswer){
        answerCounter++;
        System.out.println("Correct!");
    } else {
        System.out.println("Wrong!");
    }
}

}
我将感激你能给予的任何帮助。如果有更好的方法来完成这项任务,我很乐意看到


提前谢谢

这是您的程序的正确版本。 你那里有一些虫子。 我的程序在您发布的文件格式上运行正常 这里(不带星号)

  • 您想用*号区分正确答案和错误答案,但您没有在文本文件中提供它
  • 即使将*符号添加到正确答案的开头,也不会将正确答案值指定给
    answer
    string变量
  • 除了以上所有内容,如果你在问题下方写下正确答案的编号,为什么要用*号来检查正确答案
  • 您必须将字符串与
    equals()
    方法进行比较,因为您处理的是字符串的值,而不是字符串的内存地址
  • 您已经声明了一个StringBuilder对象以附加问题/答案并将其打印到屏幕上,但使用此代码,您将始终向当前问题添加以前的问题/答案。(即,您正在打印上面第一个的2.问题)
  • answerCounter
    变量不会保存用户的全部正确答案,因为无论何时调用该方法,您总是将其赋值为0
  • 所有这些都得到纠正后,我认为您希望实现以下目标:

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class testLoader
    {
    private static BufferedReader br;
    private static int answerCounter = 0;
    
    public static void main(String[] args)
        {
        try
            {
            br = new BufferedReader(new FileReader("C:/test.txt"));
            StringBuilder sb;
            String line, answer = null;
    
            while((line = br.readLine()) != null)
                {
                sb = new StringBuilder();
                do
                    {
                    if(line.length() == 1) //This is a bad choice of digit comparison but it will work with your case (lol)
                        {
                        answer = line;
                        line = br.readLine();
                        continue;
                        }
                    sb.append(line);
                    sb.append(System.lineSeparator());
                    line = br.readLine();
                    }
                while(!line.equals("---"));
    
                System.out.println(sb.toString());
                answerCheck(answer);
                }
            }
        catch(IOException e)
            {
            e.printStackTrace();
            }
        finally
            {
            try
                {
                br.close();
                }
            catch(IOException e)
                {
                e.printStackTrace();
                }
            }
        System.out.println("You have " + answerCounter + "correct answers");
        }
    
    public static void answerCheck(String rightAnswer)
        {
        System.out.println("What's your answer?");
        Scanner input = new Scanner(System.in);
        String answer = input.nextLine();
        if(answer.equals(rightAnswer))
            {
            answerCounter++;
            System.out.println("Correct!");
            }
        else
            {
            System.out.println("Wrong!");
            }
        }
    }
    

    运行程序时可能会发生重复的情况,以及它与您期望的情况有何不同?Thx我在equals()中看到了这一点,但代码仍然不能像我希望的那样工作。。用equals代替“!=”只显示“你的答案是什么:”并检查答案。但StringBuilder对象总是“错误”的。你应该检查:)在回答第一个问题后,它不会提示第一个问题和第二个问题吗?@mehmetgüngör谢谢你注意到这一点,它现在已经修复了。你能告诉我“sb.append(System.getProperty(“line.separator”);”和“sb.append(System.lineSeparator());”之间的区别吗为什么第一个更好?@Crypto我不认为有什么区别。但是我使用的是JDK 6,我认为这种方法是在JDK 7中引入的。所以我没有这个方法。如果它对你有用,那就没关系。所以如果你在这里检查,你会发现根本没有这样的方法
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class testLoader
    {
    private static BufferedReader br;
    private static int answerCounter = 0;
    
    public static void main(String[] args)
        {
        try
            {
            br = new BufferedReader(new FileReader("C:/test.txt"));
            StringBuilder sb;
            String line, answer = null;
    
            while((line = br.readLine()) != null)
                {
                sb = new StringBuilder();
                do
                    {
                    if(line.length() == 1) //This is a bad choice of digit comparison but it will work with your case (lol)
                        {
                        answer = line;
                        line = br.readLine();
                        continue;
                        }
                    sb.append(line);
                    sb.append(System.lineSeparator());
                    line = br.readLine();
                    }
                while(!line.equals("---"));
    
                System.out.println(sb.toString());
                answerCheck(answer);
                }
            }
        catch(IOException e)
            {
            e.printStackTrace();
            }
        finally
            {
            try
                {
                br.close();
                }
            catch(IOException e)
                {
                e.printStackTrace();
                }
            }
        System.out.println("You have " + answerCounter + "correct answers");
        }
    
    public static void answerCheck(String rightAnswer)
        {
        System.out.println("What's your answer?");
        Scanner input = new Scanner(System.in);
        String answer = input.nextLine();
        if(answer.equals(rightAnswer))
            {
            answerCounter++;
            System.out.println("Correct!");
            }
        else
            {
            System.out.println("Wrong!");
            }
        }
    }