-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestFromFile.java
More file actions
31 lines (29 loc) · 1.14 KB
/
TestFromFile.java
File metadata and controls
31 lines (29 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TestFromFile {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int trueanswer;
int trueanswerscounter = 0;
String path = "exercises.txt";
try (Scanner fr = new Scanner(new File(path))) {
while (fr.hasNextLine()) {
System.out.println(fr.nextLine());
int num = Integer.parseInt(fr.nextLine());
for (int i = 0; i < num; i++) {
System.out.println(fr.nextLine());
}
trueanswer = Integer.parseInt(fr.nextLine());
int useranswer = console.nextInt();
System.out.println(useranswer == trueanswer ? "Correct answer" : "Incorrect answer");
if (useranswer == trueanswer) trueanswerscounter++;
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
System.out.println("Total correct answers : " + trueanswerscounter);
}
}
}