• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if statement problem

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My solution keeps coming up as true and i cannot make it say false if it is not true. The question is to write a program that validates a triangle. Any two integers added together must be larger than the last number to be a triangle.








Thank you for your time.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the example where the sides are 1, 2 and 3. Since 1 + 2 == 3 this cannot form a triangle. However, 2 + 3 > 1 so your if-statements match.

Try switching it; if A + B == C, or if A + C == B, or if B + C == A, then it's not a triangle, otherwise it is.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using one input and the % operator? What happens if your triangle has sides 10, 11 and 12? Why not use three inputs to the three sides?

If you are having problems with Exceptions with Scanner try entering all three numbers in one line

10 11 12

instead of

10
11
12

. . . and ask again because there is a pitfall with Scanner which you may not be aware of.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are also testing wrongly. You want to test that a + b > c (don't use capitals or upper-case for variables) AND b + c > a AND a + c > b all together. Your application tests for them individually, and one of them will always turn out to be true.
 
Nick Hallloran
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did a little revising and it finally is starting to show false statements that are false and true statements that are true. Thank you for all of your responses. There is just one more thing, How do i enable the user to be able to input three numbers on the same line with spaces in between the numbers?


BTW In the question that I am doing in my textbook asks for all of the numbers on one line, not up and down or individually.




 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get something looking like this:

Please enter the three numbers: 3 4 5
Bingo! A real triangle

If you have problems if you enter the three numbers on separate lines, ask again. And I shan't answer because I am going away for the weekend.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic