Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import static java.lang.Math.*;
- public class Test {
- private static String isInt(Double nmb){
- Integer tmp = nmb.intValue();
- if ((nmb - tmp) == 0)
- return tmp.toString();
- return nmb.toString();
- }
- //По формуле герона
- private static double square(double a, double b, double c){
- double p = (a + b + c)/2;
- return sqrt(p * (p - a) * (p - b) * (p - c));
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Введите команду:");
- System.out.println("Найти катет - 1\n" +
- "Найти гипотенузу - 2");
- int cmd = sc.nextInt();
- switch (cmd){
- case 1:
- System.out.println("Введите гипотенузу");
- double gip = sc.nextDouble();
- System.out.println("Введите катет");
- double cat = sc.nextDouble();
- double result = sqrt(pow(gip, 2) - pow(cat, 2));
- if(Double.isNaN(result)){
- System.out.println("Треугольник не существует");
- break;
- }
- System.out.println("Второй катет: " + isInt(result));
- System.out.println("Площадь треугольника: " + isInt(square(gip, cat, result)));
- break;
- case 2:
- System.out.println("Введите катет a");
- double catA = sc.nextDouble();
- System.out.println("Введите катет b");
- double catB = sc.nextDouble();
- double res = sqrt(pow(catA, 2) + pow(catB, 2));
- if(Double.isNaN(res)){
- System.out.println("Треугольник не существует");
- break;
- }
- System.out.println("Гипотенуза : " + isInt(res));
- System.out.println("Площадь треугольника : " + isInt(square(catA, catB, res)));
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment