Write a method void triangle (int a,int b,int c) to determine whether three parameters can form a triangle. If you cannot throw an exception illegalargumentexception, the exception message is displayed: A,b,c "cannot form a triangle", or if it can be constituted, the three edge length of the triangle is displayed. In the main method, get the command line input of three integers, call this method, and catch the exception.
A<b<c
The sum of the two sides is greater than the third side: A+b>c
The difference between the two sides is less than the third side: C-a<a
Importjava.util.Arrays; ImportJava.util.Scanner; Public classtriangle Judgment { Public Static int[] side; Public Static voidMain (string[] args) {side=New int[3]; Scanner input=NewScanner (system.in); Try{side[0]=Input.nextint (); side[1]=Input.nextint (); side[2]=Input.nextint (); Arrays.sort (side); Triangle (side[0],side[1],side[2]); } Catch(IllegalArgumentException e) {e.getmessage (); E.printstacktrace (); }Catch(Exception e) {e.printstacktrace (); }finally{input.close (); } } Public Static voidTriangleintAintBintCthrowsexception{if((a+b>c) && (a-b<c)) System.out.println ("Can make a triangle"); Else Throw NewIllegalArgumentException ("Three edges cannot form a triangle"); } }
Operation Result:
Java Triangle judgment