1

This program plays craps with 3 different methods. I need help in playing craps but i'm required to have these 3 different methods but for some reason every time I compile I am getting this error:

CrapsAnalysis.java:48: error: missing return statement
    }
    ^
1 error
Process javac exited with code 1

Code:

public class CrapsAnalysis
{   
public static int rollDie( int n) {
    return (int)(Math.random()*n) + 1 ;
}
public static int rollDice( ) {
    return rollDie(6) + rollDie(6) ;
}
public static boolean playOneGame( ) {
    int newDice = rollDice();
    int roll = rollDice(); //first roll of the dice
    int playerPoint = 0; //player point if no win or loss on first roll
    if (roll == 7 || roll == 11)
        return true;
    else if (roll == 2 || roll == 3 || roll == 12)
        return false;
    else    
        playerPoint = roll;
    do {
        if (rollDice() == 7)
            return false;
        else if (rollDice() == playerPoint) 
            return true;
        else
            newDice = rollDice();
        } while (rollDice() != playerPoint || rollDice() != 7) ;
    }
}
DT7
  • 1,615
  • 14
  • 26
Angelo Mejia
  • 11
  • 1
  • 1
  • 5

8 Answers8

5

Java must look at all execution paths. What happens if the while loop ends without returning anything? You may be logically preventing that, but the Java compiler won't do that analysis.

Provide a return statement after the end of the while loop, or throw some kind of an Exception (IllegalStateException?) if the code really shouldn't ever make it there.

rgettman
  • 176,041
  • 30
  • 275
  • 357
3

You can add the last return statement after your code like this:

public static boolean playOneGame() {
    int newDice = rollDice();
    int roll = rollDice(); // first roll of the dice
    int playerPoint = 0; // player point if no win or loss on first roll

    if (roll == 7 || roll == 11)
        return true;
    else if (roll == 2 || roll == 3 || roll == 12)
        return false;
    else
        playerPoint = roll;
    do {
        if (rollDice() == 7)
            return false;
        else if (rollDice() == playerPoint)
            return true;
        else
            newDice = rollDice();
    } while (rollDice() != playerPoint || rollDice() != 7);

    return false;
}

It will make it compile and your code will still work.

Martin
  • 138
  • 2
2

you only return statements are inside the body of an if block.

The compiler doesn't know if any of those if blocks will ever be reached, so it's giving you an error.

You might want to have a default return statement at the end

    } while (rollDice() != playerPoint || rollDice() != 7) ;
    return false;
}

I'm assuming that if that default return statement ever actually gets executed, than it's an error state, and you should react accordingly.

  • So what i'm trying to do is if both conditions dont work anything else will make the dice roll again. what kind of return statement should that be? – Angelo Mejia Oct 18 '13 at 20:08
2

You are missing a return in your while block:

public static boolean playOneGame( ) {
   int newDice = rollDice();
   int roll = rollDice(); //first roll of the dice
   int playerPoint = 0; //player point if no win or loss on first roll

   if (roll == 7 || roll == 11)
      return true;
   else if (roll == 2 || roll == 3 || roll == 12)
      return false;
   else    
      playerPoint = roll;
      do {
            if (rollDice() == 7)
              return false;
            else if (rollDice() == playerPoint) 
              return true;
            else
              newDice = rollDice();

} while (rollDice() != playerPoint || rollDice() != 7) 
     **// You are missing a return statement here.**;
Brian
  • 5,069
  • 7
  • 37
  • 47
2
public static boolean playOneGame( ) {
    int newDice = rollDice();
    int roll = rollDice(); //first roll of the dice
    int playerPoint = 0; //player point if no win or loss on first roll

    if (roll == 7 || roll == 11)
        return true;
    else if (roll == 2 || roll == 3 || roll == 12)
       return false;
    else    
       playerPoint = roll;
        do {
            if (rollDice() == 7)
                return false;
            else if (rollDice() == playerPoint) 
                return true;
            else
                newDice = rollDice();
        } while (rollDice() != playerPoint || rollDice() != 7) ;
        return SOMETHING HERE;
}

You should look into consistent code formatting. Not only for us, but for yourself as well so when you look at this code after a few weeks you can still read it.

Tim
  • 222
  • 1
  • 2
  • 9
2

When you have to add return make sure you add return for every possible execution path of add a default return statement. You program is missing both of them

public class CrapsAnalysis
        {   
            public static int rollDie( int n) {
                return (int)(Math.random()*n) + 1 ;
            }
            public static int rollDice( ) {
            return rollDie(6) + rollDie(6) ;
            }
            public static boolean playOneGame( ) {
                int newDice = rollDice();
                int roll = rollDice(); //first roll of the dice
                int playerPoint = 0; //player point if no win or loss on first roll

                if (roll == 7 || roll == 11)
                return true;<--- Works
                else if (roll == 2 || roll == 3 || roll == 12)
                   return false;<--- Works
                else    
                   playerPoint = roll;
                    do {
                    if (rollDice() == 7)
                    return false;<--- Works
                    else if (rollDice() == playerPoint) 
                    return true;<--- Works
                    else
                    newDice = rollDice();     
            } while (rollDice() != playerPoint || rollDice() != 7) ;
    //No return here. You need to add a default return if none of the conditions above satisfies
        }
        }
Prateek
  • 1,916
  • 1
  • 12
  • 22
  • So what i'm trying to do is if both conditions dont work anything else will make the dice roll again. what kind of return statement should that be? – Angelo Mejia Oct 18 '13 at 20:07
  • Nothing just add a `default return` statement after the `do-while` loop to tell the compiler that if nothing works `return false` – Prateek Oct 18 '13 at 20:08
1

Here is a very stripped down version of your code:

public static boolean playOneGame()
{
    if(condition1 == true)
    {
        //code1
        return true;
    }
    else if(condition2 == true)
    {
        //code2
        return false;
    }
    else
    {
        //code3
    }
    //code4
}

If condition1 or condition2 is true, playOneGame() will return either true of false. However, if condition1 and condition2, are both false, the only code that will run is code3. code3 does not contain a return statement, so it is theoretically possible that playOneGame() will not return anything. You know that condition1 and condition2 will never both be false, but the java compiler does not, so it throws a compiler error. If it did not throw a compiler error and condition1 and condition2 somehow both became false, it would throw a runtime error. Runtime errors are a lot harder to debug than compiler errors, so the compiler is doing you a favor by throwing an easy-to-fix error.

To fix the missing return statement, add a return statement to code3 or code4.

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
0

After your while loop you need a return statement, because the if/else statements may or may not be executed. If none of them execute, you have no return. Therefore, you need to ensure that there will be at least one return statement that can always be executed.

public static boolean playOneGame( ) {
    int newDice = rollDice();
    int roll = rollDice(); //first roll of the dice
    int playerPoint = 0; //player point if no win or loss on first roll

    if (roll == 7 || roll == 11)
        return true;
    else if (roll == 2 || roll == 3 || roll == 12)
       return false;
    else    
       playerPoint = roll;
        do {
            if (rollDice() == 7)
                return false;
            else if (rollDice() == playerPoint) 
                return true;
            else
                newDice = rollDice();
        } while (rollDice() != playerPoint || rollDice() != 7) ;
    return false;
}

Also, for clarity, I rearranged your if/else statements so it is easier to read. You might want to get into the habit of doing this as well so that others can have an easier time understanding your code.

dtgee
  • 1,272
  • 2
  • 15
  • 30