注册 登录
编程论坛 JAVA论坛

写一个java的程序

米团 发布于 2016-05-25 13:29, 2108 次点击
   1.定义一个三角形类Triangle,成员属性包括三条边,能否构成三角形的标志,成员方法包括构造方法,修改三条边,计算面积
2.写一个测,类,测试自定义三角形类Triangle是否正确。写一个JAVA程序,谢谢啦
3 回复
#2
dkplus2016-05-26 17:34
这是作业吗?
#3
郑家兴2016-05-28 11:59
import java.util.*;
public class Triangle {
    public static void main(String[] args) {
        // TODO Auto-generated method stu
        Scanner in=new Scanner(System.in);
        System.out.println("The first length of a side:");
        int a=in.nextInt();
        System.out.println("The second length of a side:");
        int b=in.nextInt();
        System.out.println("The thirst length of a side:");
        int c=in.nextInt();
        Trian test=new Trian(a,b,c);
        while(true){
            test.judge();
            System.out.println("Please input the index of side and length:");
            int d=in.nextInt();
            int e=in.nextInt();
            test.change(d, e);
        }
    }
}
class Trian{
    int a;
    int b;
    int c;
    public Trian(int a,int b,int c){
        this.a=a;
        this.b=b;
        this.c=c;
    }
    public void judge (){
        if(a+b>c){
            if(b+c>a){
                if(a+c>b){
                    System.out.println("This can form a trigon!");
                    int p=(a+b+c)/2;
                    int t=p*(p-a)*(p-b)*(p-c);
                    double s=Math.sqrt(t);
                    System.out.println("This trigon area is "+s);
                }
                else{
                    System.out.println("This can't form a trigon!");
                }
            }
            else{
                System.out.println("This can't form a trigon!");
            }
        }
        else{
            System.out.println("This can't form a trigon!");
        }
    }
    public void change(int d,int e){
        switch(d){
        case 1:
            this.a=e;
            break;
        case 2:
            this.b=e;
            break;
        case 3:
            this.c=e;
            break;
        }
    }
}
#4
郑家兴2016-05-28 12:00
我也学的时间不长,很简陋的
1