Java 扫描仪问题,Notecard 类

标签 java csv java.util.scanner

我正在尝试制作一个基本上是虚拟记事卡的程序。每张记事卡都有一个问题和答案的字符串,以及目前被问过的次数的计数。我在很多情况下使用扫描仪,我认为我使用它不正确,但不太清楚为什么。该程序将让我回答前两个问题,告诉我它们无论如何都是不正确的,并跳过让我回答最后一个问题。这是记事卡类:

public class Notecard {

    public String ans;
    public String q;
    public int count;

    public Notecard(String q, String ans) {
        this.q = q;
        this.ans = ans;
        this.count = 0;
    }

    public Boolean answer(String effort) {
        if (this.q.toUpperCase().equals(effort.toUpperCase())) {
            System.out.println("Correct!");
            return true;
        } else {
            System.out.println("Incorrect! Correct answer:" + this.ans);
            count++;
            return false;
        }
    }

    public void clearCount() {
        this.count = 0;
    }

    public String getQ() {
        return this.q;
    }
}

这是我的另一个文件:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;

public class CreateNotecard {

    int trys;

    public static void main(String[] args) {
        System.out.println("Get ready to be quizzed \n\n");
        ArrayList<Notecard> notecards = makeCards();
        quiz(notecards);
    }

    static ArrayList<Notecard> makeCards() {
        ArrayList<Notecard> notecards = new ArrayList<Notecard>();
        try {
            BufferedReader in = new BufferedReader(new FileReader(
                    "notecards.txt"));
            String str;
            str = in.readLine();
            while ((str = in.readLine()) != null) {
                String[] argg = str.split(",");
                notecards.add(new Notecard(argg[0], argg[1]));
            }
            in.close();
        } catch (IOException e) {
            System.out.println("File Read Error");
        }
        return notecards;
    }

    static void quiz(ArrayList<Notecard> notecards) {
        ArrayList<Notecard> backupList = notecards;
        Scanner sc = new Scanner(System.in);
        long seed = System.nanoTime();
        Collections.shuffle(notecards, new Random(seed));
        int total = notecards.size();
        int correct = 0;
        for (Notecard x : notecards) {
            System.out.println(x.getQ());
            String effort = sc.next();
            Boolean nailedIt = x.answer(effort);
            if (nailedIt) {

                correct++;
            }
        }
        System.out.println("Total Notecards: " + total + "\nTotal Correct: "
                + correct);
        System.out.println("Accuracy: " + (correct / total));
        System.out.println("Do you want to repeat? Put \"y\" or \"n\"");
        String choice1 = sc.nextLine();
        if (choice1.toUpperCase().equals("Y")) {
            System.out.println("Use only cards missed or all? Type \"missed\" or \"all\"");
            String choice2 = sc.nextLine();
            if (choice2.toUpperCase().equals("MISSED")) {
                quiz(notecards);
            } else {
                quiz(backupList);
            }
        } else {
            return;
        }

    }

}

我有一个用于该程序的文本文件,它包含

19-9,10
square root of 4,2
capitol of Missouri,Jefferson City
Blastoise's 1st evolution,squirtle

我的输出是

Get ready to be quizzed 


square root of 4
2
Incorrect! Correct answer:2
capitol of Missouri
Jefferson City
Incorrect! Correct answer:Jefferson City
Blastoise's 1st evolution
Incorrect! Correct answer:squirtle
Total Notecards: 3
Total Correct: 0
Accuracy: 0
Do you want to repeat? Put "y" or "n"

最佳答案

您正在比较错误的事物:

public Boolean answer(String effort) {
    if (this.q.toUpperCase().equals(effort.toUpperCase())) {

应该是

    if (this.ans.toUpperCase().equals(effort.toUpperCase())) {

关于Java 扫描仪问题,Notecard 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25253713/

相关文章:

java - itext 5.5 纵向方向无法在新页面上工作

java - 如何阻止 cron 作业创建 attach_pid 文件?

php - HTML 列表到 CSV

java - 我不知道 OS X 上的路径格式是什么,所以我可以使用 Java 编写 .csv 文件

java - 在 Spring Restful Web 服务中获取 404 请求的资源不可用

java - RadioButton 值在不同 Activity 中相互重复

json - Node.js json2csv 输出未正确对齐

java - 扫描仪停止工作并且 Println 出现问题,为什么?

java - 从 System.in 获取 unicode 值