dimanche 24 mars 2019

java boolean return value in if statement not working

Im trying to make a static boolean that returns true only under specific conditions. If I put return true; inside my if statement i get an error but if I put that same code outside my if statement, there is no error.

  public static boolean higherOrLower (int n, int a, int b)
  {
    String answer;
    if (n > a)
    {
      System.out.println ("Your guess was too high, guess something lower");
      answer = Greenfoot.ask ("Your guess: ");
      n = Integer.valueOf(answer);
      b++;
    }
    if (n < a)
    {
      System.out.println ("Your guess was too low, guess something higher");
      answer = Greenfoot.ask ("Your guess: ");
      n = Integer.valueOf(answer);
      b++;
    } 
    if (n == a)
    {
        System.out.println ("Yes, correct I was thinking of the number " +a);
      if (b == 1)
      {
        System.out.println ("It took you "+b+" guess to guess my number.");
      }
      else if (b > 1)
      {
        System.out.println ("It took you "+b+" guesses to guess my number.");
      }

      return true;
     }      
}

Aucun commentaire:

Enregistrer un commentaire