创建一个三角形类,成员变量三边,方法求周长,创建类主类A来测试它。
package lianxiti; public class SanJiaoXing { double a; double b; double c; double zhouchang; SanJiaoXing(double a,double b,double c) { this.a=a; this.b=b; this.c=c; } void getZhouchang () { if(a+b>c&a+c>b&b+c>a&a!=0&b!=0&c!=0) { zhouchang=a+b+c; System.out.println("三角形的周长是:"+this.zhouchang); } else { System.out.println("您输入有误,请重新输入:"); } } }
package lianxiti; public class A { public static void main(String[] args) { SanJiaoXing sjx1 = new SanJiaoXing(2,3,4); sjx1.getZhouchang(); SanJiaoXing sjx2 = new SanJiaoXing(1,3,4); sjx2.getZhouchang(); } }