user1792457
user1792457

Reputation: 21

Boolean in a method not returning correctly (array formal paramater)

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;
  }
}

Upvotes: 1

Views: 221

Answers (2)

Jordan Kaye
Jordan Kaye

Reputation: 2887

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;
    }

Upvotes: 3

kosa
kosa

Reputation: 66637

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.

Upvotes: 7

Related Questions