This is my simple game that gives you a country and has you enter the corresponding capital. What do you think? (Before running it you need to do score.txt.)package com.company;import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;import java.util.Random;import java.util.Scanner;public class Main {    public static void main(String[] args) throws FileNotFoundException{        Random r = new Random();        Scanner input = new Scanner(System.in);        String[] capital = {Lisbon, Madrid, Paris, Berlin, Warsaw, Kiev, Moscow, Prague, Rome};        String[] country = {Portugal, Spain, France, Germany, Poland, Ukraine, Russia, Czech Republic, Italy};        Boolean[] was = new Boolean[9];        int score = 0;        int good = 0;        int bad = 0;        File file = new File(score.txt);        Scanner lastscoreopener = new Scanner(file);        String lastscore = lastscoreopener.nextLine();        System.out.print(Your last score was:  +lastscore +\n);        for (int d = 1; d<4; d++) {            int randomint = r.nextInt(9);            while (was[randomint] == Boolean.FALSE){                randomint = r.nextInt(9);            }            was[randomint] = false;            while (was[randomint] == true);            for (int x = 0; x < 3; x++) {                System.out.print(What's capital of  + country[randomint] + \n);                String answer = input.nextLine();                if (answer.toLowerCase().equals(capital[randomint].toLowerCase())) {                    if (x==0){                        score = score+3;                        good++;                    }                    else if(x==1){                        score = score+2;                        good++;                    }                    else if (x==2){                        score = score+1;                        good++;                    }                    System.out.print(Good. Your score is +score +. \n);                    break;                } else {                    if (x != 2){                        System.out.print(Bad. You have  +(2-x) + chances. );}                    if (x == 2){                        System.out.print(Bad. Good answer is +capital[randomint] +\n);                    }                    bad++;                }            }        }        System.out.print(End of game. Good answers: +good +. Bad answers:  +bad +. Score:  +score);        PrintWriter saver = new PrintWriter(score.txt);        saver.println(score);        saver.close();    }}
Capitals and countries game
java;beginner;quiz
Match what you say to what you are actually doing        for (int d = 1; d<4; d++) {You want to loop three times.  Why does it say 4? Either         for (int d = 1; d <= 3; d++) {or more idiomatically         for (int d = 0; d < 3; d++) {Since you never use d except for iteration, there seems no reason to start from 1 instead of 0.  Use helper objectsclass Country {    private final String name;    private final String capital;    public Country(String capital, String name) {        this.name = name;        this.capital = capital;    }    public String getName() {        return name;    }    public String getCapital() {        return capital;    }}Now you can say List<Country> countries = new ArrayList<>();countries.add(new Country(Lisbon, Portugal));Same pattern for the other countries.  Now if you add a new country, you can't accidentally add the capital to a different index than the country name.  Consider making this a class field private static final List<Country> countries = new ArrayList<>();static {    countries.add(new Country(Lisbon, Portugal));Don't forget to close the static initializer block after adding all the countries and capitals.  }Either way, you can then say List<Country> choices = new ArrayList<>(countries);You use it like Country choice = choices.remove(r.nextInt(choices.size()));Now you don't need a was variable.  This is self-maintaining.  It only picks from the entries still in the list.  Also, if you add an entry to the list, it will automatically include it.  You don't have to go through your code changing all your magic numbers to match each other.  Use methods to hide complexity        File file = new File(score.txt);        Scanner lastscoreopener = new Scanner(file);        String lastscore = lastscoreopener.nextLine();        System.out.print(Your last score was:  +lastscore +\n);The point of these four lines is to output one line to the screen.  It creates three variables to do so which are never used again.  So hide that in a method.  Either     private static final String SCORE_FILE_NAME = score.txt;    public void displayLastScore() {         File file = new File(SCORE_FILE_NAME);         try (Scanner scanner = new Scanner(file)) {             System.out.println(Your last score was:  + scanner.nextLine());         }    }Or with better separation of concerns     private static final String SCORE_FILE_NAME = score.txt;    public String readLastScore() {         File file = new File(SCORE_FILE_NAME);         try (Scanner scanner = new Scanner(file)) {             return scanner.nextLine();         }    }which you use like          System.out.println(Your last score was:  + readLastScore());The latter is generally recommended, as it leaves readLastScore more flexible.  Either version will close the Scanner when done, as that's what the try-with-resources form does.  It ensures that the AutoCloseable resource is closed before leaving the try block.  