Java Scanner问题,Notecard类

时间:2014-08-11 22:56:34

标签: java csv java.util.scanner

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

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"

2 个答案:

答案 0 :(得分:2)

你正在比较错误的事情:

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

应该是

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

答案 1 :(得分:1)

问题是Scanner类正在寻找用于创建令牌的分隔符,默认情况下为空格。由于您输入&#34; 2&#34;,Scanner.next()找不到分隔符,因此没有令牌。

例如,如果您输入&#34; Jefferson City&#34;,Scanner找到一个分隔符,那么两个令牌。在这种情况下,sc.next将是&#34; Jefferson&#34;只有(没有&#34;城市&#34;,那是下一个标记)。

解决方案?从stdin读取行并使用sc.nextLine()