vendredi 3 avril 2020

java: illegal start of type/ not a statement error (for loop, if statement)

I'm coding a simple math game (bluej, java) where the user is asked 5 random math questions using an if statement inside of a for loop. They are generated through Math.random and the user is given a percentage of how many questions they got right at the end.

For some reason, in the last couple of lines, there are errors such as "illegal start of type" and "not a statement". and I can't figure out what (or where) the error is.

public class withIF
{
    public static void main (String args [] )
{ 
    new withIF ();
}

public withIF ()
{
    int points = 0;
    int num1 = 0;
    int num2 = 0;

    for (int i = 1; i<6; i++)
    {
    num1 = ((int) (Math.random () * 5)) + 1;
    num2 = ((int) (Math.random () * 5)) + 1;
    int ans = IBIO.inputInt ("Answer this question: " + num1 + " + " + num2 + " = ");
    if (ans == (num1 + num2))
    { 
    System.out.println ((num1 + num2) + " is correct! You got it right!");
    points++;
}
else
{
    System.out.println ((num1 + num2) + " is the correct answer, not " + ans);
    System.out.println ("Score: " + points);
}
}
System.out.println (" ");
int final = points * 20; //here is the error!
System.out.println ("You got " +final+ "% of questions correct"); //here is the error!
}
}

Aucun commentaire:

Enregistrer un commentaire