mardi 27 février 2018

Boolean used with If statement

I just started learning Java at school and I encountered the following piece of code. I have trouble understanding why the output shows: 'no'. Since 'x' is updated to '100' shouldn't the boolean also update to 'false' and therefore output: 'yes' ?

Thank you in advance

Sorry for any format mistake, this is my first post.

   public static void main (String[] args)
   {
       Scanner keyboard = new Scanner(System.in);
       int x = -555;
       boolean isNegative = (x < 0);
       if (isNegative)
       {
           x = 100;
           if (isNegative)
             System.out.println("no");
          else
             System.out.println("yes");
       } 
       else
          System.out.println("maybe");

  }

Aucun commentaire:

Enregistrer un commentaire