dimanche 15 avril 2018

Java, Access variable outside IF-statement

I cant get this program to compile. Is there anyone who sees what I am doing wrong? After the if-statement it seems like the variable matchOdds don't exist. But I want to print it.

    /*
    Simple program to print out the game-odds of the result the user chooses in a match; either home, draw or away.
     */

    //Asks the user what he thinks the result is:
    String match = showInputDialog("Choose home(H), draw(D) or away(A):");

    //Converts the answer from string to char:
    char matchRead = match.charAt(0);

    //Depending on what the user thinks the result is, the match-odds is taken from one of the three if-statements:
    double matchOdds;
    if (matchRead == 'H') {
        matchOdds = 1.20;
    } else if (matchRead == 'U') {
        matchOdds = 2.30;
    } else if (matchRead == 'B') {
        matchOdds = 3.45;
    }

    //And then I wanted it to print the odds, i.e. 2.30 if the user chose 'U'...But it dosent compile because "Variable matchOdds might not have been initialized":
        System.out.println(matchOdds);

Aucun commentaire:

Enregistrer un commentaire