给出五组三角形三条边,然后判断每个三角形什么类型。用什么语句能让五组数据一次性输入,然后依次运算,最后依次出结果

解决方案 »

  1.   

    可以通过匹配来拆分字符串,下面是代码,写得不太好public class TestInput
    {
    public static void main(String[] args)
    {

    float[][] triangles;//用二维数组保存数据

    Scanner input = new Scanner(System.in);
    System.out.println("请输入数据(每组数据之间用分号隔开," +
    "组内数据用逗号隔开,按回车键结束):");


    String s = input.nextLine();//读取一行数据

    /*
     * 先根据分号来分割,对数据进行分组,再根据逗号对每组数据
     * 进行分割,获得相应的边长
     */
    String[] strTriangles = s.split(";");
    triangles = new float[strTriangles.length][];//确定三角形个数
    for (int i = 0; i < strTriangles.length; i++)
    {
    String[] strSides = strTriangles[i].split(",");
    triangles[i] = new float[3];
    for (int j = 0; j < 3; j++)
    {
    //对三边进行赋值
    triangles[i][j] = Float.parseFloat(strSides[j]); 
    }
    }

    //打印检验
    System.out.println("你输入的三角形分别是:");
    for (int i = 0; i < triangles.length; i++)
    {
    for (int j = 0; j < 3; j++)
    {
    System.out.print(triangles[i][j] + " ");
    }
    System.out.println();//打印完一个换行
    }
    }
    }
      

  2.   

    题目在pdf文档里 怎么传上来啊
      

  3.   

    虽然这样是不对的,但是我还是决定发了public class TestInput
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    System.out.println("请输入五组数据,数据之间用空格隔开");

    //将数据存储在二维数组中
    float[][] datas = new float[5][3];
    for (int i = 0; i < datas.length; i++)
    {
    for (int j = 0; j < datas[i].length; j++)
    {
    datas[i][j] = input.nextFloat();
    }
    }

    float a, b, c;//记录三角形三边
    for (int i = 0; i < datas.length; i++)
    {
    Arrays.sort(datas[i]);//对每个三角形三边进行排序

    a = datas[i][0];//最短边
    b = datas[i][1];
    c = datas[i][2];//最长边

    if (a + b <= c)//判断能否构成三角形
    {
    System.out.println("第" + (i+1) + "个不能构成三角形");
    continue;
    }
    if ((a == b) || (a == c) || (b == c))//判断是否是等边或等腰三角形
    {
    if ((a == b) && (a == c))//等边三角形
    {
    System.out.println("第" + (i+1) + "个是等边三角形");
    continue;
    }

    System.out.println("第" + (i+1) + "个是等腰三角形");
    continue;
    }
    System.out.println("第" + (i+1) + "个是一般三角形");
    }
    }
    }
      

  4.   


    /**
     * 由于题意模糊,只判断锐角三角形,直角三角形,钝角三角形
     */
    public class JudgeTriangleType {
        public static void main(String[] args) {
    judgeTriangleType(5, 5, 5);
    judgeTriangleType(3, 4, 5);
    judgeTriangleType(5, 4, 3);
    judgeTriangleType(5, 1, 2);
    judgeTriangleType(2, 2.9, 3.9);
    judgeTriangleType(5,4,3.0);
        }    public static void judgeTriangleType(double x, double y, double z) {
    Triangle triangle = Triangle.createTriangle(x, y, z);
    if (triangle != null)
        System.out.println(triangle);
    else
        System.out.println("输入错误");
        }
    }class Triangle {
        private double a, b, c;    private Triangle(double x, double y, double z) {
    this.a = x;
    this.b = y;
    this.c = z;    }
        public static Triangle createTriangle(double x, double y, double z) {
    if (x + y > z && x + z > y)
        return new Triangle(x, y, z);
    else
        return null;// 是否合法三角形
        }
        public String toString() {
    double x = a;
    double y = b;
    double z = c;
    if (z < x) {
        double temp = x;
        x = z;
        z = temp;
    }
    if (z < y) {
        double temp = y;
        y = z;
        z = temp;
    }
    if (x * x + y * y < z * z)
        return x + "  " + y + "  " + z + "  " + "是钝角三角形";
    if (x * x + y * y > z * z)
        return x + "  " + y + "  " + z + "  " + "是锐角三角形";
    else
        return x + "  " + y + "  " + z + "  " + "是直角三角形";
        }
    }
    /*
    5.0  5.0  5.0  是锐角三角形
    3.0  4.0  5.0  是直角三角形
    3.0  4.0  5.0  是直角三角形
    2.0  1.0  5.0  是钝角三角形
    2.0  2.9  3.9  是钝角三角形
    3.0  4.0  5.0  是直角三角形 
    */