java三角形判定代码 软件测试java三角形形状判定

java判断三角形类型package demo;
import java.util.Scanner;
public class Triangle
{
/**
* 等腰
*/
public static final int ISOSCELES = 1;
/**
* 等边
*/
public static final int EQUILATERAL = 2;
/**
* 直角
*/
public static final int RIGHTANGLE = 4;
/**
* 普通
*/
public static final int GENERAL = 6;
/**
* 不构成三角形
*/
public static final int NOTHING = 7;
double a;
double b;
double c;
public Triangle ( double a, double b, double c )
{
this.a = a;
this.b = b;
this.c = c;
【java三角形判定代码 软件测试java三角形形状判定】 }
public static int isWhichOne ( Triangle triangle )
{
double a = triangle.a;
double b = triangle.b;
double c = triangle.c;
if (abcacbbcaMath.abs (a - b)cMath.abs (a - c)bMath.abs (b - c)a)
{
if (a == ba == c)
{
return Triangle.EQUILATERAL | Triangle.ISOSCELES;
}
else if (a * ab * b == c * c || a * ac * c == b * b || c * cb * b == a * a)
{
if (a == b || a == c || b == c)
{
return Triangle.RIGHTANGLE | Triangle.ISOSCELES;
}
return Triangle.RIGHTANGLE;
}
else if (a == b || a == c || b == c)
{
return Triangle.ISOSCELES;
}
else
{
return Triangle.GENERAL;
}
}
else
{
return Triangle.NOTHING;
}
}
public static double perimeter ( Triangle triangle )
{
return triangle.atriangle.btriangle.c;
}
public static double area ( Triangle triangle )
{
double a = triangle.a;
double b = triangle.b;
double c = triangle.c;
return 1.d / 4 * Math.sqrt (( abc ) * ( ab - c ) * ( ac - b ) * ( bc - a ));
}
public static void translate ( Triangle triangle )
{
int type = isWhichOne (triangle);
switch (type)
{
case Triangle.ISOSCELES:
System.out.println ("等腰三角形");
break;
case ( Triangle.EQUILATERAL | Triangle.ISOSCELES ):
System.out.println ("等边三角形等腰三角形");
break;
case Triangle.GENERAL:
System.out.println ("普通三角形");
break;
case ( Triangle.RIGHTANGLE | Triangle.ISOSCELES ):
System.out.println ("直角三角形等腰三角形");
break;
case Triangle.RIGHTANGLE:
System.out.println ("直角三角形");
break;
default:
System.out.println ("不构成三角形");
break;
}
if (type != Triangle.NOTHING)
{
System.out.println ("周长是java三角形判定代码:"perimeter (triangle));
System.out.println ("面积是java三角形判定代码:"area (triangle));
}
}
public static void main ( String[] args )
{
System.out.println ("输入构成三角形的三边长java三角形判定代码:(空格隔开)\n退出按 (n/N), 否则继续: ");
String error = "输入有误java三角形判定代码,请重新输入: ";
String reg = "^\\d \\s \\d \\s \\d $";
Scanner scanner = new Scanner (System.in);
while (scanner.hasNextLine ())
{
String line = scanner.nextLine ().trim ();
if ("n".equalsIgnoreCase (line))
{
break;
}
if (!line.matches (reg))
{
System.err.println (error);
}
else
{
try
{
String[] input = line.split ("\\s ");
int a = Integer.parseInt (input[0]);
int b = Integer.parseInt (input[1]);
int c = Integer.parseInt (input[2]);
Triangle triangle = new Triangle (a, b, c);
translate (triangle);
}
catch (NumberFormatException e)
{
System.err.println (error);
}
System.out.println ("退出按 (n/N), 否则继续: ");
}
}
scanner.close ();
}
}
java初学者,判断是否为三角形 , 代码如下,提示我缺少类class Triangle {
int a;
int b;
int c;
public Triangle() {
}
public Triangle(int a, int b, int c) { // 把无关java三角形判定代码的语句去掉
this.a = a;
this.b = b;
this.c = c;
}
public void isTriangle() { // 用这个方法判断是不是三角形
if ((this.athis.bthis.c)(this.athis.cthis.b)
(this.bthis.cthis.a)) {
System.out.println("java三角形判定代码我是一个三角形");
} else
System.out.println("我不是一个三角形");
}
}
public class Test {
public static void main(String[] args) {
Triangle t = new Triangle(2, 3, 1);
t.isTriangle();
}
}
如何用java代码来做三角形的判断?只要能获得三角形的三边长度,就很容易了
假设三边长度为a、b、c
先判断是否为三角形:|a-b|ca b(三个边)
钝角、锐角用余弦定理,判断cosA(三个点)的正负性
直角用勾股定理
等腰或等边更简单
关于java三角形判定代码和软件测试java三角形形状判定的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读