This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes if statement problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if statement problem" Watch "if statement problem" New topic
Author

if statement problem

Nick Hallloran
Greenhorn

Joined: Feb 09, 2010
Posts: 19
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39087
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 39087
    
  23
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

Joined: Feb 09, 2010
Posts: 19
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
Sheriff

Joined: Oct 13, 2005
Posts: 39087
    
  23
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.
 
GeeCON Prague 2014
 
subject: if statement problem