I am trying to write a guessing game that repeatedly picks a random object from an array and compares it to user input (gives the user the country name and has them guess the capital) but I'm relatively new to coding and I can't seem to figure out how to get a random object from the array and how to read the data from said object. Here is my code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int yesNo;
CountryCard a = new CountryCard();
a.setName("Canada");
a.setCapital("Ottawa");
CountryCard b = new CountryCard();
b.setName("United States");
b.setCapital("Washington");
CountryCard c = new CountryCard();
c.setName("Denmark");
c.setCapital("Copenhagen");
CountryCard d = new CountryCard();
d.setName("Egypt");
d.setCapital("Cairo");
CountryCard e = new CountryCard();
e.setName("Finland");
e.setCapital("Helsinki");
CountryCard f = new CountryCard();
f.setName("Germany");
f.setCapital("Berlin");
CountryCard g = new CountryCard();
g.setName("Thailand");
g.setCapital("Bangkok");
CountryCard h = new CountryCard();
h.setName("Syria");
h.setCapital("Damascus");
CountryCard i = new CountryCard();
i.setName("Uganda");
i.setCapital("Kampala");
CountryCard j = new CountryCard();
j.setName("Latvia");
j.setCapital("Riga");
CountryCard k = new CountryCard();
k.setName("Japan");
k.setCapital("Tokyo");
CountryCard l = new CountryCard();
l.setName("China");
l.setCapital("Beijing");
CountryCard m = new CountryCard();
m.setName("Costa Rica");
m.setCapital("San Jose");
CountryCard n = new CountryCard();
n.setName("Brazil");
n.setCapital("Brasilia");
CountryCard o = new CountryCard();
o.setName("New Zealand");
o.setCapital("Wellington");
CountryCard[] game = {a, b, c, d, e, f, g, f, i, j, k, l, m, n, o};
//I will need to implement some form of a loop here
System.out.println("Would you like to guess the capital of a country?"
+ " Hit 1 for yes, or 2 for no");
yesNo = input.nextInt();
if (yesNo != 1) {
System.out.println("Goodbye :(");
System.exit(2);
}
if (yesNo == 1) {
// this portion should be the guessing game,
// get data from array compare
}
}//end of main
// template for the playing cards
class CountryCard {
//name of the country
String name;
//capital of the country
String capital;
//has the card been used or not
Boolean used = false;
//number of instances created within the class
static int instances = 0;
//sets name for a card
public void setName(String name) {
this.name = name;
instances += 1;
}//end of setName
//gets the name of a card, returns name
public String getName() {
return name;
}//end of getName
//set capital for a card
public void setCapital(String capital) {
this.capital = capital;
instances += 1;
}//end of setCapital
//gets capital of a card, returns capital
public String getCapital() {
return capital;
}//end of getCapital
//determines whether or not card has
public boolean usedCard(boolean used) {
//been used, returns boolean value
return used;
}
//returns the amount of instances in class
public int getInstances() {
return instances;
}//end of getInstances
}//end of class country card