重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
class Triangle {
创新互联专注于企业网络营销推广、网站重做改版、赤峰林西网站定制设计、自适应品牌网站建设、H5技术、商城网站定制开发、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为赤峰林西等各大城市提供网站开发制作服务。
int a;
int b;
int c;
public Triangle() {
}
public Triangle(int a, int b, int c) { // 把无关的语句去掉
this.a = a;
this.b = b;
this.c = c;
}
public void isTriangle() { // 用这个方法判断是不是三角形
if ((this.a + this.b this.c) (this.a + this.c this.b)
(this.b + this.c this.a)) {
System.out.println("我是一个三角形");
} else
System.out.println("我不是一个三角形");
}
}
public class Test {
public static void main(String[] args) {
Triangle t = new Triangle(2, 3, 1);
t.isTriangle();
}
}
只要能获得三角形的三边长度,就很容易了
假设三边长度为a、b、c
先判断是否为三角形:|a-b|ca+b(三个边)
钝角、锐角用余弦定理,判断cosA(三个点)的正负性
直角用勾股定理
等腰或等边更简单
if ("true".equals(show.isTriangle(a, b, c))) {
改成
if (show.isTriangle(a, b, c)) {
if ((a + b c) (a + c b) (b + c a)) {
改成
if (((a + b) c) ((a + c) b) ((b + c) a)) {
你再试试
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;
}
public static int isWhichOne ( Triangle triangle )
{
double a = triangle.a;
double b = triangle.b;
double c = triangle.c;
if (a + b c a + c b b + c a Math.abs (a - b) c Math.abs (a - c) b Math.abs (b - c) a)
{
if (a == b a == c)
{
return Triangle.EQUILATERAL | Triangle.ISOSCELES;
}
else if (a * a + b * b == c * c || a * a + c * c == b * b || c * c + b * 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.a + triangle.b + triangle.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 (( a + b + c ) * ( a + b - c ) * ( a + c - b ) * ( b + c - 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 ("周长是:" + perimeter (triangle));
System.out.println ("面积是:" + area (triangle));
}
}
public static void main ( String[] args )
{
System.out.println ("输入构成三角形的三边长:(空格隔开)\n退出按 (n/N), 否则继续: ");
String error = "输入有误,请重新输入: ";
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 ();
}
}