Results 1 to 6 of 6
  1. #1
    adz06 is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default 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. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Just use

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

  4. #4
    adz06 is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    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. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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. #6
    adz06 is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    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

Similar Threads

  1. What am I doing wrong in this basic problem
    By Blaedel in forum New To Java
    Replies: 10
    Last Post: 10-11-2009, 08:45 PM
  2. Drawing out triangles
    By leiferouis in forum New To Java
    Replies: 24
    Last Post: 01-16-2009, 09:18 PM
  3. Triangles
    By CodeDog in forum New To Java
    Replies: 9
    Last Post: 10-14-2008, 10:18 PM
  4. [SOLVED] very basic java problem
    By sales1 in forum New To Java
    Replies: 13
    Last Post: 08-20-2008, 09:33 PM
  5. asterisks triangles
    By Dan121 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 08:42 PM

Posting Permissions

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