函数题:处理IllegalTriangleException异常
创建一个Triangle类,包括三角形三边的数据域以及返回周长的getPerimeter()方法。
属性:side1,side2,side3,都是整数。
构造方法:public Triangle(int side1, int side2, int side3) throws IllegalTriangleException
用于初始化side1,side2,side3的值。如果不满足任意两条边的和大于第三条边且三条边的边长都必须大于0的条件,则抛出一个自定义的IllegalTriangleException异常。
方法:public int getPerimeter()
返回三角形的周长。
创建一个IllegalTriangleException类(继承自Exception类)。
构造方法:public IllegalTriangleException(int side1, int side2, int side3)
生成一条异常消息 “IllegalTriangleException: The sum of any two sides must greater than the other side and the sides must greater than zero,side1,side2,side3 can not contruct a triangle”。
### 裁判测试程序样例:
c++
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
while (n > 0) {
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
try {
Triangle t1 = new Triangle(a, b, c);
System.out.println(t1.getPerimeter());
} catch (IllegalTriangleException ex) {
System.out.println(ex);
}
n--;
}
}
}
/* 请在这里填写答案 */
### 输入格式:
先输入一个整数n,表示有n组数据。
每组包含3个整数,表示三角形三边。
### 输出格式
若三边不符合要求,抛出IllegalTriangleException异常,并打印异常描述;
若均符合要求,输出三角形周长。
### 输入样例:
in
3
1 2 3
3 3 3
4 5 6
### 输出样例:
out
IllegalTriangleException: The sum of any two sides must greater than the other side and the sides must greater than zero,1,2,3 can not contruct a triangle
9
15
答案:若无答案欢迎评论
属性:side1,side2,side3,都是整数。
构造方法:public Triangle(int side1, int side2, int side3) throws IllegalTriangleException
用于初始化side1,side2,side3的值。如果不满足任意两条边的和大于第三条边且三条边的边长都必须大于0的条件,则抛出一个自定义的IllegalTriangleException异常。
方法:public int getPerimeter()
返回三角形的周长。
创建一个IllegalTriangleException类(继承自Exception类)。
构造方法:public IllegalTriangleException(int side1, int side2, int side3)
生成一条异常消息 “IllegalTriangleException: The sum of any two sides must greater than the other side and the sides must greater than zero,side1,side2,side3 can not contruct a triangle”。
### 裁判测试程序样例:
c++
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
while (n > 0) {
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
try {
Triangle t1 = new Triangle(a, b, c);
System.out.println(t1.getPerimeter());
} catch (IllegalTriangleException ex) {
System.out.println(ex);
}
n--;
}
}
}
/* 请在这里填写答案 */
### 输入格式:
先输入一个整数n,表示有n组数据。
每组包含3个整数,表示三角形三边。
### 输出格式
若三边不符合要求,抛出IllegalTriangleException异常,并打印异常描述;
若均符合要求,输出三角形周长。
### 输入样例:
in
3
1 2 3
3 3 3
4 5 6
### 输出样例:
out
IllegalTriangleException: The sum of any two sides must greater than the other side and the sides must greater than zero,1,2,3 can not contruct a triangle
9
15
答案:若无答案欢迎评论