首页 > 代码库 > Triangle Problems
Triangle Problems
Triangle Problem
songxiuhuan 宋修寰
Import the Junit and eclemma
Choose the project and right click, choose the build path and choose the Junit and hamcrest.
Install eclemma
Help——install newsoftware——input the URL of the eclemma in my PC
Coding the triangle and testTriangle, to verify if it’s a triangle and equilateral, isosceles, or scalene.
When a==b==c it’s a equilateral one.
When a==b!=c it’s a isosceles one.
Others they are scalene.
KEY: A regular triangle must obey that a+b>c and a-b<c. Or there will be a failure.
package testTriangle; import static org.junit.Assert.*; import java.util.Arrays; import java.util.Collection; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import triangle.triangle; @RunWith(Parameterized.class) public class testTriangle { private int a; private int b; private int c; private String expected; public testTriangle(int a,int b, int c, String expected){ this.a = a; this.b = b; this.c = c; this.expected= expected; } @Parameters public static Collection<Object[]> getData(){ return Arrays.asList(new Object[][]{ {3,3,3,"equilateral"}, {1,2,3,"scalene"}, {5,5,7,"isosceles"}, {4,6,6,"isosceles"} }); } @Test public void test() { assertEquals(this.expected,triangle.triangleshape(a,b,c)); } }
package triangle; public class triangle { public static String triangleshape(int a,int b, int c){ if( (a + b) < c||(a - b) > c){ return "error"; }//判断是否符合三角形三条边的情况,即两边之和大于第三边,两边之差小于第三边; if(a == b && a == c && b == c){ return "equilateral"; }//三条边相等即为等边三角形 else if(a == b || a == c || b == c){ return "isosceles"; }//任意两边相等即为等腰三角形,并且等边三角形也是等腰三角形 else{ return "scalene"; }//否则为不等边三角形 } }
Triangle Problems
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。