Results 1 to 5 of 5
  1. #1
    Legoland is offline Member
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    12
    Rep Power
    0

    Default What value will a and b have??

    Hi!
    Im completley stuck on this dont know where to begin???
    What value will a have and b after the below lines has been executed?

    float x = 1, y = 2;
    float a = x + 15 / y - 6*x/y*2/3;
    float b = (x+15) / (y-6)) * x/ (y*2) /3;

  2. #2
    levent Guest

    Default

    You have mistyped the expression for b there. There is a missing left parenthesis or extra right parenthesis there.

    And this is mostly operator precedence. Where are you stuck?

  3. #3
    derrickD is offline Member
    Join Date
    Apr 2007
    Location
    USA
    Posts
    50
    Rep Power
    0

    Default

    I changed the code float b = to this because it did not compile:
    Java Code:
    float b = (x+15) / (y-6) * x/ (y*2) /3;
    Once I did that then this line:
    System.out.println("B= "+ b +" A = "+a);
    gave this out put:
    B= -0.33333334 A = 6.5
    If you are looking for a certain output maybe you need to put () around what you want to calculate first.
    What output are you expecting?

  4. #4
    Legoland is offline Member
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    12
    Rep Power
    0

    Default Thanks both

    I will continue to work on this tomorrow

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all, it's better to read about operator precedences. levent already gave a link. Did you read it? Then exactly tell us what you are expecting? Depends on that, what you are looking to do, have to use parenthesis in correct order.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •