import java.util.Scanner;
public class Test{
private static double length;//边长
private static double bottom;//底
private static double tall;//高
private String perimeter;
private String area;
//无构造器
private Test(){}
//有构造器
private void Test(double length,double bottom,double tall){
this.length=length;
this.bottom=bottom;
this.tall=tall;}
//set和get方法
public void setlength(double length){
this.length=length;}
public void setbottom(double bottom){
this.bottom=bottom;}
public void settall(double tall){
this.tall=tall;}
public double getlength(){
return this.length;}
public double getbottom(){
return this.bottom;}
public double gettall(){
return this.tall;}
//周长和面积
public double getperimeter(){
return length+length+bottom;}
public double getarea(){
return bottom*tall/2;}
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入三角形的边长");
length = sc.nextDouble();
System.out.println("请输入三角形的底");
bottom = sc.nextDouble();
System.out.println("请输入三角形的高");
tall = sc.nextDouble();
System.out.println("三角形的周长是:"+new Test().getperimeter()+"三角形的面积是"+new Test().getarea());
}
}
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 ();
}
}