-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
executable file
·45 lines (40 loc) · 1.17 KB
/
Game.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.io.Console;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
class Game {
public static void main(String[] args) {
System.out.println("Guess the capital city");
System.out.println("type 'exit' to stop the game");
Console console = System.console();
int total = 0;
int correct = 0;
try (BufferedReader br = new BufferedReader(new FileReader("capitals.csv"));){
String line;
while ((line = br.readLine()) != null) {
String[] cols = line.split(",");
String country = cols[0];
String capital = cols[1];
String answer = console.readLine("What is the capital of " + country + "? ");
if (answer.equalsIgnoreCase("exit")){
break;
}
if (answer.equalsIgnoreCase(capital)) {
System.out.println("Correct!");
correct++;
}
else {
System.out.println("No, the answer is " + capital);
}
total++;
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("You scored " + correct + " out of " + total);
}
}