This site requires JavaScript, please enable it in your browser!
Greenfoot back
ipeefreely
ipeefreely wrote ...

2015/5/18

why is this not the answer I thought it was

ipeefreely ipeefreely

2015/5/18

#
After the following Java code has be executed, what is the value of bonus? bonus = 0; sales = 500; if ( sales > 300 ) bonus = bonus0 + 20; else if ( sales > 400 ) bonus = bonus0 + 30; else if ( sales > 600 ) bonus = bonus0 + 40; Shouldnt I use the bonus for the last one making the answer 40 since thats the only if statement that was true? the multiple choice answers were A) 20; B) 0; C) 30; D)40; or E)90 Ithe only other answer I could think it could be is 0 but I still dont understand why when the others were false statements
azazeel azazeel

2015/5/18

#
those statement will be make logical error if ( sales > 300 ) if sales 500 bonus value will be 20, since 500 is true under condition sales > 300 my suggestion and a little correction :
bonus = 0;
sales = 500;
if ( sales > 600 )
	bonus = bonus + 40;
else if ( sales > 400 ) 
	bonus = bonus + 30;
else if ( sales > 300 ) 
	bonus = bonus + 20;
Super_Hippo Super_Hippo

2015/5/18

#
@azazeel: I think this code was given and you have to find out the value of 'sales'. If you change the given code like you did, you will get a different result. @ipeefreely: You used 'bonus0' in the code above. If 'bonus0' is 0 or 'bonus', then at the end, 'bonus' will be 20. The reason is the following: 'if (sales>300)' checks if 'sales', which is 500, is greater than 300. It is, so it executes the if-block (which is the next line). So 'bonus' is set to 'bonus0+20'. The next condition isn't even checked because it has this 'else' before the 'if'. That means it would only check the condition if the if-condition before that ('sales>300' in this case) would have returned 'false'.
You need to login to post a reply.