标签:
创建一个三角形类,成员变量三边,方法求周长,创建类主类A来测试它。
private int a; private int b; private int c; public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int getC() { return c; } public void setC(int c) { this.c = c; } void qiuZhouChang() { if(a+b>c&&a+c>b&&c+b>a&&a-b<c&&a-c<b&&b-a<c&&b-c<a&&c-a<b&&c-b<a) { System.out.println("三角形的周长是:"+(a+b+c)); } else { System.out.println("这三条边无法组成一个三角形"); } } public static void main(String[] args) { SanJiaoXing sanjiaoxing=new SanJiaoXing(); sanjiaoxing.setA(2); sanjiaoxing.setB(1); sanjiaoxing.setC(1); System.out.println("三角形的三条边长分别是"+sanjiaoxing.getA()+" "+sanjiaoxing.getB()+" "+sanjiaoxing.getC()); sanjiaoxing.qiuZhouChang(); }
运行结果
标签:
原文地址:http://www.cnblogs.com/mutougezi/p/5513998.html