Joshua Sears
Joshua Sears

Reputation: 17

Reading an array using a randomly generated number

I am trying to write a program that stores countries and there capitals in two separate arrays and then get the user to guess the country based on the capital. The user can enter 3 countries and if one of them matches the capital the program should print the statement "Your answer is correct".

I am needing to use a randomly generated number to choose which capital is displayed.

The problem I am having is that no matter if the country is right or wrong, I am always getting a different amount of statements returned, both correct and incorrect. It's like the random number is changing at each if statement.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in); 

    String[] Countries = {"Greece", "Germany", "Thailand", "Netheralnds", "China", "Turkey"};
    String[] Capitals = {"Athens", "Berlin", "Bangkok", "Amsterdam", "Beijing", "Ankara"};

    int RandomNum = ThreadLocalRandom.current().nextInt(0, 6);
    out.print("Which country has the capital " + Capitals[RandomNum] + "?\n");
    out.print("Enter up to 3 names, comma-seperated: ");
    String UsrCountry = input.next();

    if (RandomNum == 0)
        if (UsrCountry.contains("Greece")) {
            out.print("Your answer is correct");
        }
        else {
            out.print("Your answer is incorrect");
        }
    if (RandomNum == 1)
        if (UsrCountry.contains("Germany")) {
            out.print("Your answer is correct");
        }
        else {
            out.print("Your answer is incorrect");
        }
    if (RandomNum == 2)
        if (UsrCountry.contains("Thailand")) {
            out.print("Your answer is correct");
        }
        else {
            out.print("Your answer is incorrect");  
        }
    if (RandomNum == 3)
        if (UsrCountry.contains("Netherlands")) {
            out.print("Your answer is correct");
        }
        else {
            out.print("Your answer is incorrect");
        }
    if(RandomNum == 4)
        if (UsrCountry.contains("China")) {
            out.print("Your answer is correct");
        }
        else {
            out.print("Your answer is incorrect");
        }
    if (RandomNum == 5)
        if (UsrCountry.contains("Turkey")) {
            out.print("Your answer is correct");
        }
        else {
            out.print("Your answer is incorrect");
        }
    }
}

Sample Output:

    Which country has the capital Athens?
Enter up to 3 names, comma-seperated: Greece
Your answer is correctYour answer is incorrectYour answer is incorrectYour answer is incorrectYour answer is incorrectYour answer is incorrect

Upvotes: 1

Views: 120

Answers (4)

Razzlero
Razzlero

Reputation: 1193

For an if statement to work it has to be followed by either another statement (with no semicolon in between), or a code block (code between curly braces). If the check in the if statement is true then it will perform the code following the check.

The issue is that most of your if statements have a semi colon directly after the if condition check. for example you have

if (RandomNum == 1);

But it should just be

if (RandomNum == 1)

The semicolon after the statement is closing the if statement without doing anything. Then the line after the if statement is being performed with no relation to the previous if statement. To fix it remove the colons directly after the condition.

Also some general advice. Your code has a lot of repetition. This looks like homework, but in real systems this would increase the amount of work needed to make changes, and increase the changes of mistakes being made in future. For example you are printing the message "Your answer is correct" multiple times. If you wanted to update that message in future, you would need to change it in multiple places. There would also be a risk that you would miss one of the places where it needs to be changed.

Upvotes: 0

Andrew
Andrew

Reputation: 49626

The problem is that you have incorrect ifs, remove a semicolon after the conditions.

The valid if-else is

if (RandomNum == 2) { ... } else { ... }

not

if (RandomNum == 2); { ... } else { ... }

Also, the if statements could be replaced with:

out.println("Your answer is " + 
    (UsrCountry.contains(Countries[RandomNum]) ? "correct" : "incorrect"));

Upvotes: 0

Nitin Sharma
Nitin Sharma

Reputation: 100

use this line of code for optimize.

 if (UsrCountry.contains(Countries[RandomNum])){
     out.print("Your answer is correct");
  }
  else {
     out.print("Your answer is incorrect");
   }

if use this code then no need to multiple if-else condition.

Upvotes: 0

Michael
Michael

Reputation: 44150

You have typos here:

if (RandomNum == 1);
if (RandomNum == 2);
//...

The semi-colon means the if block has no body (i.e. it does nothing). Any competent IDE should warn you when you do this.

Remove the semi-colons. I'd also personally recommend that you always place braces around if blocks:

if (RandomNum == 2) {
    if (UsrCountry.contains("Thailand")) {
        out.print("Your answer is correct");
    }
    else {
        out.print("Your answer is incorrect");  
    }
}

Upvotes: 3

Related Questions