Thread: Triangles java problem (basic help)

1. Member
Join Date
Oct 2009
Posts
8
Rep Power
0

Triangles java problem (basic help)

Hello basically i am wondering how if the triangle isnt valid to get the program to terminate and not countinue to work out what type of triangle it is.

It looks a bit stupid when i shows up
"this is not a valid triangle"
"This is a scalene triangle"

Program so far:- (this program compiles and works fine i just want to figure out how to do the above and how to modify it)

Java Code:
```public class 	Triangles4
{
public static void main(String[] args)
{
int side_a = getScannerInput.anInt("Please enter a positive integer to represent side a. ");
int side_b = getScannerInput.anInt("Please enter a positive integer to represent side b. ");
int side_c = getScannerInput.anInt("Please enter a positive integer to represent side c. ");

System.out.println ("The values you entered were " + side_a + "," + side_b + "," + side_c + ".");
{
if((side_a+side_b>side_c)&&(side_a+side_c>side_b)&&(side_c+side_b>side_a))
System.out.println ("The values," + side_a + "," + side_b + "," + side_c + " could form the sides of a triangle.");

else
System.out.println ("The values,"+ side_a + "," + side_b + "," + side_c + " could NOT form the sides of a triangle.");}

if(side_a==side_b&&side_b==side_c&&side_c==side_a)
System.out.println ("This would form and Equilateral Triangle");

else if ((side_a==side_b&&side_a!=side_c)||(side_b==side_c&&side_b!=side_a)||(side_c==side_a&&side_c!=side_b))
System.out.println ("This would form an Isosceles Triangle");

else if ((side_a!=side_b)&&(side_b!=side_c)&&(side_c!=side_a))
System.out.println ("This would form an Scalene Triangle");

}
}```
Thanks
Adam
p.s im new to java as in only been using it for 2 weeks.
Last edited by adz06; 10-31-2009 at 07:54 PM.

2. if the triangle isnt valid
How would you define this? What would make a triangle invalid?
How about: if the sum of any two sides is less than the third side?

3. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
Just use

Java Code:
`System.exit(0);`
to stop the program.

4. Member
Join Date
Oct 2009
Posts
8
Rep Power
0
Originally Posted by hardwired
if the triangle isnt valid
How would you define this? What would make a triangle invalid?
How about: if the sum of any two sides is less than the third side?
Yes that is the theory i am currently using.

System.exit(0); Wont work due to the fact that if the triangle is valid i DO NOT want the program to terminiate i would it to continue and tell me what type of triangle it is.

5. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
Java Code:
```else {
System.out.println ("The values,"+ side_a + "," + side_b + "," + side_c + " could NOT form the sides of a triangle.");
System.exit(0);
}```
P.S Format your code and use code tags when posting code.

6. Member
Join Date
Oct 2009
Posts
8
Rep Power
0
Originally Posted by r035198x
Java Code:
```else {
System.out.println ("The values,"+ side_a + "," + side_b + "," + side_c + " could NOT form the sides of a triangle.");
System.exit(0);
}```
P.S Format your code and use code tags when posting code.
Thanks very much was trying brackets around the full "if" statement and not the else.
And sorry about posting inccorectly i will know for again.
Thanks
Adam

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•