簡體   English   中英

Java掃描儀問題,Notecard類

[英]Java Scanner issues, Notecard class

我正在嘗試制作一個基本上是虛擬便簽紙的程序。 每個記錄卡都有一個字符串,用於表示問題和答案,以及現在多次被問到的計數。 我在許多情況下都使用掃描儀,但我認為使用不正確,並且不確定原因。 該程序將讓我回答前兩個問題,告訴我無論如何它們都是不正確的,並跳過讓我回答最后一個問題。 這是筆記卡類:

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())) {

問題在於Scanner類正在尋找定界符來創建令牌,默認情況下是空白 由於您輸入“ 2”,所以Scanner.next()找不到分隔符,因此也沒有標記。

例如,如果輸入“ Jefferson City”,則Scanner找到一個定界符,因此找到兩個標記。 在這種情況下, sc.next僅是“ Jefferson”(沒有“ City”,這是下一個標記)。

解? 從stdin讀取行並使用sc.nextLine()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM