Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Problems with int

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I*ve wrote following program:



When I run the program, it always says "Result: You won a bronze Medal and 20$" although you won a gold Medal. Same is when I actually won a silver Medal. How can I fix it?
 
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or change the signs from <= to >=
 
Marshal
Posts: 14386
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason that happens is because the checks will happen one after another from top to bottom. Since it's an if-else-if-else structure, that means the check for m <= 900 happens first. Think about it, if m is 500 then the first successful check will be that if (m <= 900) since 500 is less than 900.  If you're going to keep the conditional expressions you're using, you need to check if m <= 780 first because if it's not, then it must be greater than 780. Next, you'd check if m is in the next tier, m <= 840. If it's not, then only would you check if it's <= 900.

The question now is: What should your program do if m > 900? Right now it will do nothing.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ! your code goes something like this here,

 
Marshal
Posts: 66237
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Purvi Barot wrote:. . .

Can you see the potential errors in lines 5/8 and 8/11? Why is there that difference between those pairs of lines?
 
Carey Brown
Saloon Keeper
Posts: 6419
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 

 
Campbell Ritchie
Marshal
Posts: 66237
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PB: did you find what errors I was trying to point out?
I prefer to use < and > rather than ≤ and ≥ because I think the simpler operators are easier to read. In this case I would write the cases in the opposite order:Note there is no need for an if in the last else.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this way!!

 
Campbell Ritchie
Marshal
Posts: 66237
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sherin Mathew wrote:Try this way!! . . .

No. You have too many tests; you don't need to mention 840 twice. Each occurrence of 840 is an opportunity for an error to occur, and mixing > and <= provides you with more opportunities to introduce errors.
What happens if you get a mark of 901?
 
I think I'll just lie down here for a second. And ponder this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!