• Post Reply Bookmark Topic Watch Topic
  • New Topic

Are any of these wrong?  RSS feed

 
Alvaroo Hernandez
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
FullSizeRender.jpg
[Thumbnail for FullSizeRender.jpg]
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post text instead of an image?

Also please post the manual steps you took to find the answers. For example:
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the fourth one may be incorrect.... and BTW, please QuoteYourSources.

Henry
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that is java code, why not write a small testing program that can be executed and show you the answer?
 
Alvaroo Hernandez
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When i run this it outputs nothing anybody know why? i ran the others and they where right except for this one 
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are missing {} following some of the if statements.  Edit the code and add the missing {}s so you can see the code's logic better.

Note: you should always use {}s with if statements and loops and anywhere a block of code can be controlled by a single statement.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alvaroo Hernandez wrote:
When i run this it outputs nothing anybody know why? i ran the others and they where right except for this one 


There is no output because none of the conditions are satisfied. This includes the condition that sets the value to 99. This is why your answer to the fourth one is incorrect.

Henry
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
none of the conditions are satisfied
'
Depending on what if  the else is connected to.   Missing {}s can make the code harder to read and understand.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
Depending on what if  the else is connected to.   Missing {}s can make the code harder to read and understand.


Perhaps... but the OP's code does match the picture provided (in the initial post), which we have to assume is correct.

And BTW, to the OP, again, please QuoteYourSources.

Henry
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Depending on what if  the else is connected to
'else' being connected to nearest 'if' in case of missing curly braces.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alvaroo Hernandez wrote:
When i run this it outputs nothing anybody know why? i ran the others and they where right except for this one 

I don't know if you realize what is happening on lines 11, 13, 16, and 18.  The expression that you are passing to System.out.print() is an assignment expression which is first executed. The value passed to print() is the value assigned to d. It appears to work but there is one flaw in this: when the condition on line 15 is false, execution does not fall through to the else on line 17 because that else is part of the if-statement on line 14. You probably thought otherwise and expected line 18 to be executed, so you answered 99.  In this case, none of the System.out.print() statements will be executed so you will not get any output. 

If you do this instead, you will get the output that will show you the answer for any case.

 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like its the case of the missing else.
 
Alvaroo Hernandez
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!! i can see where it went wrong since the value for d was already established and since none of the statements were satisfied it should be 44. I feel dumb right now how did i not catch that   Thanks again everyone as i am barely learning java i will get the hang of it with time 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!