Java扫描仪问题,记事卡类
我正在尝试制作一个基本上是虚拟记事卡的程序。每张便笺卡上都有一个字符串,表示一个问题和一个答案,还有一个计数,因为它已经被问了很多次了。我在很多情况下使用扫描仪,我认为我使用它不正确,不太清楚为什么。该程序将让我回答前两个问题,告诉我它们无论如何都是错误的,并跳过让我回答最后一个问题。下面是notecard类:Java扫描仪问题,记事卡类,java,csv,java.util.scanner,Java,Csv,Java.util.scanner,我正在尝试制作一个基本上是虚拟记事卡的程序。每张便笺卡上都有一个字符串,表示一个问题和一个答案,还有一个计数,因为它已经被问了很多次了。我在很多情况下使用扫描仪,我认为我使用它不正确,不太清楚为什么。该程序将让我回答前两个问题,告诉我它们无论如何都是错误的,并跳过让我回答最后一个问题。下面是notecard类: public class Notecard { public String ans; public String q; public int count;
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;
}
}
}
我的输出是
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”,所以Scanner.next()
找不到分隔符,因此没有标记
例如,如果输入“Jefferson City”,扫描器会找到一个分隔符,因此会找到两个令牌sc.next
在这种情况下,只有“杰斐逊”(没有“城市”,这是下一个标记)
解决方案?从stdin中读取该行,并使用
sc.nextLine()
问题在于扫描器
类正在寻找用于创建令牌的分隔符,默认情况下,该分隔符为空白。由于您输入了“2”,所以Scanner.next()
找不到分隔符,因此没有标记
例如,如果输入“Jefferson City”,扫描器会找到一个分隔符,因此会找到两个令牌sc.next
在这种情况下,只有“杰斐逊”(没有“城市”,这是下一个标记)
解决方案?从stdin中读取该行,并使用
sc.nextLine()
问题在于扫描器
类正在寻找用于创建令牌的分隔符,默认情况下,该分隔符为空白。由于您输入了“2”,所以Scanner.next()
找不到分隔符,因此没有标记
例如,如果输入“Jefferson City”,扫描器会找到一个分隔符,因此会找到两个令牌sc.next
在这种情况下,只有“杰斐逊”(没有“城市”,这是下一个标记)
解决方案?从stdin中读取该行,并使用
sc.nextLine()
问题在于扫描器
类正在寻找用于创建令牌的分隔符,默认情况下,该分隔符为空白。由于您输入了“2”,所以Scanner.next()
找不到分隔符,因此没有标记
例如,如果输入“Jefferson City”,扫描器会找到一个分隔符,因此会找到两个令牌sc.next
在这种情况下,只有“杰斐逊”(没有“城市”,这是下一个标记)
解决方案?阅读stdin中的行,并使用
sc.nextLine()
比较错误的内容:
public Boolean answer(String effort) {
if (this.q.toUpperCase().equals(effort.toUpperCase())) {
应该是
if (this.ans.toUpperCase().equals(effort.toUpperCase())) {
你在比较错误的东西:
public Boolean answer(String effort) {
if (this.q.toUpperCase().equals(effort.toUpperCase())) {
应该是
if (this.ans.toUpperCase().equals(effort.toUpperCase())) {
你在比较错误的东西:
public Boolean answer(String effort) {
if (this.q.toUpperCase().equals(effort.toUpperCase())) {
应该是
if (this.ans.toUpperCase().equals(effort.toUpperCase())) {
你在比较错误的东西:
public Boolean answer(String effort) {
if (this.q.toUpperCase().equals(effort.toUpperCase())) {
应该是
if (this.ans.toUpperCase().equals(effort.toUpperCase())) {
下一行是更好的选择吗?是的,我应该澄清一下。nextLine应该能解决你的问题。nextLine会是更好的选择吗?是的,我应该澄清一下。nextLine应该能解决你的问题。nextLine会是更好的选择吗?是的,我应该澄清一下。nextLine应该能解决你的问题。nextLine会是更好的选择吗?是的,我应该澄清一下。下一行应该能解决你的问题。天啊,谢谢你抓住了那个。天啊,谢谢你抓住了那个。天啊,谢谢你抓住了那个。天啊,谢谢你抓住了那个。