日期:2014-05-20 浏览次数:20966 次
public class Test { public static int a, b, c; public static void main(String arg[]) throws IOException { try { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("输入三边值,每个值输入后回车"); System.out.println("请输入:"); a = Integer.valueOf(stdin.readLine()); b = Integer.valueOf(stdin.readLine()); c = Integer.valueOf(stdin.readLine()); checkTriangle(a,b,c); } catch (IOException e) { System.out.println("出现异常!"); System.exit(0); } } public static void checkTriangle(int a,int b,int c){ if (a + b < c || a + c < b || b + c < a) { System.out.println("你输入的不能构成三角形"); } if (a == b || a == c || b == c) { if (a == b && b == c) System.out.println("等边三角形"); else System.out.println("等腰三角形"); } else { System.out.println("一般三角形"); } if(a+b+c>30) { System.out.println("三角形周长是" +(a+b+c)); }else { System.out.println("三角形三边长分别是:" + a + " " + b + " " + c); } } }
------解决方案--------------------
package com.test; import java.util.Scanner; public class Triangle { int a , b , c; public Triangle(int a , int b , int c){ this.a = a; this.b = b; this.c = c; } //是否为三角形(两边之和大于第三边,之差小于第三边) public boolean isTri(){ return a + b > c && Math.abs(a - b) < c; } //是否为等边三角形(三边相等) public boolean isEquilateralTri(){ return a == b && b == c; // } //是否为等腰三角形(两边相等) public boolean isIsoscelesTri(){ return a == b || b == c; } //是否为一般三角形(三边都不相等) public boolean isGeneralTri(){ return a != b && b != c; } public static void main(String[] args) { int a = 0, b = 0 , c = 0; System.out.println("*********请输入三条边值,每个值输入后回车********"); Scanner can = new Scanner(System.in); System.out.print("第一条边:"); a = can.nextInt(); System.out.print("第二条边:"); b = can.nextInt(); System.out.print("第三条边:"); c = can.nextInt(); Triangle tri = new Triangle(a ,b, c); if(tri.isTri()){ System.out.println("******你输入的是个三角形******"); if(tri.isEquilateralTri()){ System.out.println("你输入的三条边为:"+a+" "+b+" "+c+" 为等边三角形"); }else if(tri.isIsoscelesTri()){ System.out.println("你输入的三条边为:"+a+" "+b+" "+c+" 为等腰三角形"); }else if(tri.isGeneralTri()){ System.out.println("你输入的三条边为:"+a+" "+b+" "+c+" 为一般三角形"); } }else{ System.out.println("******你输入的不是三角形******"); } } }