Problems assigning variable to the return of a function

I have been searching online for solutions to this problem for hours, but cannot find one. I am a beginner with java so I don't understand many of the errors and this is why I wasn't able to correct this myself.

Anyway, I am trying to assign a value to variable and then add the int value from the variable to another. When compiling I am getting errors: "this is not a statement" and an error that I missing a semicolon which I suspect will be cleared up if I fix the statement.

Here is the error, just in case you need it:

hw8_ex2_jm2rv.java:61: error: ';' expected
        int sumOfGames += value;
                      ^
hw8_ex2_jm2rv.java:61: error: not a statement
        int sumOfGames += value;
                          ^
2 errors

This is my code:

public static int oneGame()
{
    int trueValue = 1;
    int falseValue = 0;
    int total = sumTwoDice();
    if((total == 7) || (total == 11))
    {
        //System.out.println(trueValue  + " " + total);
        return(trueValue);
    }
    else if((total == 2) || (total == 3) || (total == 12))
    {
        //System.out.println(falseValue + " " + total);
        return(falseValue);
    }
    else
    {
        int total2 = 0;
        while((total2 != 7) || (total2 != total))
        {
            total2 = sumTwoDice();
            if(total2 == total)
            {
                //System.out.println(trueValue + " " + total + " " + total2);
                return(trueValue);

            }
            else if(total2 == 7)
            {
                //System.out.println(falseValue + " " + total + " " + total2);
                return(falseValue);

            }
        }
    }

}

You are probably wondering about the sumTwoDice method, all it does is return a number between 2 and 12 and I already know that it is working.If you want to see it I can post it too, just ask.

The oneGame function just simulates a game of craps and returns an integer value 1 or 0. With 1 being a win and 0 being a loss.

//this method runs a simulation of n values and returns the avg.
public static double monteCarloSim() 
{
    int trueValue = 1;
    int falseValue = 0;
    int sumOfGames = 0;
    int n = 2;
    for (int i = 0; i <= n; i++)
    {
        int value = oneGame();
        int sumOfGames += value;

    }
    double avg = sumOfGames / n;
    return(avg);
}

I just have a small n at the moment so that it is easier to deal with and faster to compile.

My main question is about what makes this statement invalid and what can I do do fix it, but also since many of the threads don't cover this on stack overflow, What makes a statement invalid in general. Hopefully this will help both me and other beginners that don't understand this error.

Thanks in advance for your help and knowledge!

Jon Skeet
people
quotationmark

Just remove the int part of

int sumOfGames += value;

So it becomes

sumOfGames += value;

You can't use a compound assignment operator in a variable declaration - the whole point of a compound assignment operator is that it uses the previous value. Even if you could do that, it would clash with the previous declaration of sumOfGames in the same method.

As a matter of style, it's worth noting that you're using more brackets than you need to - sometimes that can aid readability, but it can also hurt it. For example, instead of:

if((total == 7) || (total == 11))
{
    //System.out.println(trueValue  + " " + total);
    return(trueValue);
}

I would write:

if (total == 7 || total == 11)
{
    //System.out.println(trueValue  + " " + total);
    return trueValue;
}

As for what makes a statement invalid in general - it's if it violates the Java Language Specification rules in some way. There are lots of different ways that can happen, ranging from lexical and syntactic errors (things that simply don't make any sense, like x..y in an expression) to things that are syntactically valid, but semantically invalid (e.g. trying to invoke a method that doesn't exist, or trying to invoke a method that does exist, but where the call is ambiguous due to multiple overloads being valid but no one overload being better than the others).

people

See more on this question at Stackoverflow