我试图编写一个从用户那里获取文件名的程序(例如:英文),然后打开这个文件并打印(9个问题)分为(3个级别,每个级别有3个问题) ,然后打开包含答案的另一个文件(EnglishC
),然后将其与正确答案进行比较。如果正确grade++
。
输出:
enter your choice:
1.English
2.Math
3.Science
java.io.FileNotFoundException: English.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.util.Scanner.<init>(Scanner.java:636)
at Generater.createQuestions(Generater.java:50)
at Generater.choose_subject_And_Level(Generater.java:41)
at Generater.main(Generater.java:139)
来源:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Generater {
static int levelNo = 1;
static int subjectName;
static int grade;
static String fileName;
static String fileNameCorrect;
static String ans;
static String correctAns;
public static void choose_subject_And_Level() {
// here the main will call this method to ask the user what subject to be tested in?
Scanner input = new Scanner(System.in);
System.out.println("enter your choice:\n 1.English\n2.Math\n3.Science ");
subjectName = input.nextInt();
switch (subjectName) {
case 1:
fileName = "English.txt";
fileNameCorrect = "EnglishC.txt";
break;
case 2:
fileName = "Math.txt";
fileNameCorrect = "MathC.txt";
break;
case 3:
fileName = "Science.txt";
fileNameCorrect = "SienceC.txt";
break;
}
createQuestions(fileName, fileNameCorrect, levelNo);
}
public static void createQuestions(String fileName, String fileNameCorrect,
int levelNo) {
Scanner input, input2;
try {
input = new Scanner(new File(fileName));
input2 = new Scanner(new File(fileNameCorrect));
input.useDelimiter("*");
FileInputStream fs = new FileInputStream(fileName);
FileInputStream fs2 = new FileInputStream(fileNameCorrect);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
BufferedReader br2 = new BufferedReader(new InputStreamReader(fs2));!
while (input.hasNext()) {
switch (levelNo) {
case 1:
for (int i = 1,k=1; i <= 3 ; i++,k++) {
System.out.printf("\nQ#\f: " + input.next() + "\n" , k);
ans = input.next();
correctAns = input2.next();
if (ans == correctAns) {
grade++;
}
}
break;
case 2:
for (int i = 4, k = 1; i <= 6; i++, k++) {
try {
for (int j = 1; j <= 3; j++) {
br.readLine();
correctAns = br2.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.printf("\nQ#\f: " + input.next() + "\n" , k);
ans = input.next();
if (ans == correctAns) {
grade++;
}
}
break;
case 3:
for (int i = 7, k = 1; i <= 9; i++, k++) {
try {
for (int j = 1; j <= 6 ; j++){
br.readLine();
correctAns = br2.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.printf("\nQ#\f: " + input.next() + "\n" , k);
ans = input.next();
correctAns = input2.next();
if (ans == correctAns) {
grade++;
}
}
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
// the main
public static void main(String[] args) {
choose_subject_And_Level();
}
}
答案 0 :(得分:1)
当抛出java.io.FileNotFoundException时,它意味着应该读取/写入的文件不存在于指定的位置。查看文件系统上文件的位置,并确认存在相关文件(English.txt)。
此处有更多信息:https://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html