日期:2014-05-20  浏览次数:20883 次

求!!关于用java判断输入3个数组成什么三角形
有谁能帮帮我吗?帮忙把这个程序里面的判断 是否是三角形、等边三角形 等腰三角形
一般三角形 4个功能分别有用函数封装起来!!(就是main函数调用它们那种)  
求大神啊 急用 全部分在这了
import java.io.*;
class sanj{
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());
}catch(IOException e){
System.out.println("出现异常!");
System.exit(0);
}
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);


------解决方案--------------------
不需要弄出四个方法吧,判断是什么三角型的统一抽出来放在一个方法就行了,main方法去调用它。
------解决方案--------------------
构造一个判断类
在main中可以用Judge.a()的形式来调用相关函数
函数体楼主可以自行修改

public class Judge {

public static void a(int a,int b,int c){
if(a+b<c || a+c<b || b+c<a){
System.out.println("你输入的不能构成三角形");
}
}

public static void b(int a,int b,int c){
if(a==b || a==c || b==c)
{
if(a==b && b==c) System.out.println("等边三角形");
}

}

public static void c(int a,int b,int c){
if(a==b || a==c || b==c)
{
if(a==b && b==c){

}
else{
System.out.println("等腰三角形");
}
}
}

public static void d(int a,int b,int c){
if(a==b || a==c || b==c){

}
else{
System.out.println("一般三角形");
}
}

}

------解决方案--------------------
Java code
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);
        }
    }
}

------解决方案--------------------
Java code

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("******你输入的不是三角形******");
        }
    }

}