I can't find out what is wrong with my code. Please Help!
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");
}
}
Re: I can't find out what is wrong with my code. Please Help!
You have written
when you intended
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.
Re: I can't find out what is wrong with my code. Please Help!
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");
}
}
Re: I can't find out what is wrong with my code. Please Help!
Oh wait, now I see why what you're doing is wrong. For A, B, C to form a triangle, it has to be true that A+B>C, A+C>B and B+C>A. In other words, all three have to be true. The reason your program was never falling through to the final else statement is that at least one of those inequalities will always be true.
As for the other point, even if all the input is supposed to be on one line, the user should be able to enter any three positive integers, separated by (say) spaces.