• February 9th, 2010, 09:54 PM
hallor618
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.
Code :

```import java.util.Scanner;   public class Triangle { public static void main(String[] args) { Scanner input = new Scanner(System.in);   System.out.print("Enter three edges:"); int Sides = input.nextInt();   int C = Sides % 10; int RemainingNumber = Sides / 10; int B = RemainingNumber % 10; RemainingNumber = RemainingNumber / 10; int A = RemainingNumber % 10;   if (A + B > C && A + B != C) System.out.println("Can edges" + A + "," + B + ", " + C + "form a triangle?" + "true");   else if (A + C > C && A + C != C) System.out.println("Can edges" + A + "," + B + ", " + C + "form a triangle?" + "true");   else if (B + C > C && B + C != C) System.out.println("Can edges" + A + "," + B + ", " + C + "form a triangle?" + "true");   else System.out.println("Can edges" + A + "," + B + ", " + C + "form a triangle?" + "false");       } }```
• February 10th, 2010, 02:55 AM
Shambolic
You have written
Code :

`else if(A+C>C)`
when you intended
Code :

`else if(A+C>B)`
and similarly for B+C.

Also, your code can be simply improved. The second half of each of your if statements is redundant. If, for example, A+B>C is true, then of course A+B=C is false. Finally, your program only makes sense if the user enters three integers less than 10. Why not ask for the three edge lengths separately?

And please put code tags around your code, and indent it sensibly.
• February 10th, 2010, 03:15 AM
hallor618
I added the changes you told me about. However, it still shows formulas as being true. I entered 1,4,9. Also I thought the same thing about making the user be able to enter each integer individually, but the question specifically asks for three integers on one line. Also I looked up how to put code tags on my code, but I could not seem to figure it out while I was posting.

Code :

```import java.util.Scanner;   public class Triangle { public static void main(String[] args) { Scanner input = new Scanner(System.in);   System.out.print("Enter three edges:"); int Sides = input.nextInt();   int C = Sides % 10; int RemainingNumber = Sides / 10; int B = RemainingNumber % 10; RemainingNumber = RemainingNumber / 10; int A = RemainingNumber % 10;   if (A + B > C) System.out.println("Can edges" + A + ", " + B + " , " + C + "form a triangle?" + "true");   else if (A + C > B) System.out.println("Can edges" + A + "," + B + " , " + C + "form a triangle?" + "true");   else if (B + C > A) System.out.println("Can edges" + A + " , " + B + " , " + C + "form a triangle?" + "true");       else System.out.println("Can edges" + A + " , " + B + " , " + C + "form a triangle?" + "false");       } }```
• February 10th, 2010, 01:44 PM
Shambolic