Demo entry 6643776

编写一个Java应用程序,用户在键盘输入3个数,程序判断这3个数能构成什么形状的三角形。

   

Submitted by anonymous on Sep 29, 2017 at 04:47
Language: Java. Code size: 1.4 kB.

// 编写一个Java应用程序,用户在键盘输入3个数,程序判断这3个数能构成什么形状的三角形。
import java.util.Scanner;
public class D02 {
    public static void main(String args[]) {
        boolean i = false;
        int x[] = new int[4];
        int temp;
        ONE:while (!i)
        {
            int m=0;
            System.out.print("请输入三个整数用于判断这三个数能构成什么形状的三角形:\n");
            Scanner reader = new Scanner(System.in);
            for (m=0;m<3; m++)
            {
                x[m] = reader.nextInt();
                if(x[m] <= 0)break;
            }
            if (m == 3) break ONE;
            System.out.print("输入错误,请重新输入!\n");
        }
        for (int m = 1; m < 3; m++) {
            for (int n = m; n > 0; n--) {
                if (x[n] < x[n - 1]) {
                    temp = x[n];
                    x[n] = x[n + 1];
                    x[n + 1] = temp;
                }
            }
        }
        if ((x[2] < x[1] + x[0]) && (x[2] - x[1] < x[0])) {
            if (x[0] * x[0] + x[1] * x[1] < x[2] * x[2]) System.out.printf("%d,%d,%d构成锐角三角形", x[0], x[1], x[2]);
            else {
                if (x[0] * x[0] + x[1] * x[1] == x[2] * x[2]) System.out.printf("%d,%d,%d构成直角三角形", x[0], x[1], x[2]);
                else System.out.printf("%d,%d,%d构成钝角三角形", x[0], x[1], x[2]);
            }
        } else System.out.printf("%d,%d,%d不构成三角形", x[0], x[1], x[2]);
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).