1

I am trying to make a cee-lo program in simple, simple java. I'm just learning. However when I get to my instant w. (i have simplified it for the test) it just always returns false. I can't seem to figure out why. It even displays the correct data but when it compares it it fails.

public class ceeLo
{
  public static void main (String [] args)
  {
    Scanner scan= new Scanner (System.in);
    int [] die = new int [3];
    int answer;
    boolean roll = true;
    boolean qualifed;
    boolean instantW;
    boolean instantL;

    do
    {
      System.out.println("Roll the dice?");
      answer = scan.nextInt ();
      if (answer == 0)
        roll= false;
      else
      {
        int i;
        for (i = 0; i < die.length; i++)
        {
          die[i]= rollin();
          System.out.println(diceTxt(die[i]));  
        }

        qualifed = (qualify (die));
        System.out.println("Qualified = " + qualifed);
        instantW = (easyW (die));
        System.out.println("Instant win = " + instantW);
      }
    }
    while (roll);
  }

  // Generate random numbers for the roll
  public static int rollin ()
  {
    Random rand = new Random();
    int die= rand.nextInt(6);
    return die;
  }

  //Check if dice qualify with pair
  public static boolean qualify (int [] die)
  {
    boolean qualify;
    //Pair Qualifying roll
    if (die[0] == die[1] || die[0] == die[2] || die[1] == die[2])
      qualify = true;
    else
      qualify = false;
    return qualify;
  }

  //Check if instant win 
  public static boolean easyW (int [] die)
  {
    boolean instantW;
    // show contents of die [x] for testing
    System.out.println (die[0] + "" + die[1] + "" + die[2]);
    if (die[0] > 2 && die [1] > 2 && die[2] > 2)
          instantW = true;
    else;
      instantW = false;
    return instantW;
  }
}
2
  • 1
    First there is a semicolon after the last else than shouldn't be here, and are those three values in easyW superior to 2? Commented Nov 1, 2012 at 20:09
  • Good catch @talnicolas. Always use curly braces around blocks of code. And format your code in your IDE to detect such problems. Note that this if/else block can be replaced by instantW = (die[0] > 2 && die [1] > 2 && die[2] > 2);. Commented Nov 1, 2012 at 20:10

2 Answers 2

7

Remove semi-colon after else; it should be just else

I guess the reason is,

instantW = false; is being treated as separate statement not part of else block. Which is why instantW is always being assigned to false and returning false.

It is always better to use {} to define block even though they are single liners. It is my preference.

As Greg Hewgill suggested, using single statement instantW = die[0] > 2 && die [1] > 2 && die[2] > 2; would do good than if/else.

Sign up to request clarification or add additional context in comments.

2 Comments

+1. Even better, instantW = die[0] > 2 && die [1] > 2 && die[2] > 2; would be simpler than an if statement.
Then comparably, the method could just return die[0] > 2 && die [1] > 2 && die[2] > 2; instead of even setting up a new variable. Just another idea.
3

A better way to write boolean methods is really to do something like

boolean easyW(int[] die)
{
   return (die[0] > 2 && die[1] > 2 && die[2] > 2);
}

Or even better (more general)

boolean easyW(int[] die)
{
   for(int roll : die)
   {
       if(roll < 2)
       {
          return false;  
       }
   }
   return true;
}

But in your case, you have a ; after your else. Fixed version:

  public static boolean easyW (int [] die)
    {
        boolean instantW;
      // show contents of die [x] for testing
      System.out.println (die[0] + "" + die[1] + "" + die[2]);
      if (die[0] > 2 && die [1] > 2 && die[2] > 2)
            instantW = true;
         else
            instantW = false;
      return instantW;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.