标签:href htm 生成 否则 img 设计 als boolean int
编写三角形问题,注意输入3条边的长度需要满足三角形定义,才能进行下一步判断和输入的必选是整数值,否则抛出异常
package st;
public class triangle {
private boolean caculate(int a, int b, int c) {
if(a+b>=c && a-b<=c)
return true;
else
return false;
}
public Boolean isTriangle(int a, int b, int c) {
boolean result = true;
result = caculate(a, b, c);
result = caculate(a, c, b);
result = caculate(c, b, a);
return result;
}
public boolean isEquilateral(int a, int b, int c) {
if(isTriangle(a, b, c) == false) return false;
if(a == b && b == c)
return true;
else
return false;
}
public boolean isIsosceles(int a, int b, int c) { if(isTriangle(a, b, c) == false) return false; if(a == b || b == c || a == c) return true; else return false; }
public boolean isScalene(int a, int b, int c) { if(!isEquilateral(a, b, c)&& !isIsosceles(a, b, c)) return true; else return false; } }
设计测试环节,可以右键new->junit test case自动生成,或者自己在建立一个文件夹然后新建立一个包包名和测试的类一致
package st;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
public class JuintTest {
private static triangle testTriangle = new triangle();
@Before //每次运行test都会执行该方法
public void setUp()throws Exception{
} @Test //测试案例 public void testIsEquilateral(){ assertEquals(true, testTriangle.isEquilateral(5, 5, 5)); } @Test public void testIsIsosceles(){ assertEquals(true, testTriangle.isIsosceles(5, 5, 5)); } @Test public void testIsScalene(){ assertEquals(true, testTriangle.isScalene(4, 5, 6)); } } 测试结果:

标签:href htm 生成 否则 img 设计 als boolean int
原文地址:http://www.cnblogs.com/faith30/p/6535379.html