weydile228

У

Jan 18th, 2019
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. import static java.lang.Math.*;
  4.  
  5.  
  6. public class Test {
  7.  
  8.     private static String isInt(Double nmb){
  9.         Integer tmp = nmb.intValue();
  10.         if ((nmb - tmp) == 0)
  11.             return tmp.toString();
  12.         return nmb.toString();
  13.     }
  14.  
  15.     //По формуле герона
  16.     private static double square(double a, double b, double c){
  17.         double p = (a + b + c)/2;
  18.         return sqrt(p * (p - a) * (p - b) * (p - c));
  19.     }
  20.  
  21.     public static void main(String[] args) {
  22.         Scanner sc = new Scanner(System.in);
  23.         System.out.println("Введите команду:");
  24.         System.out.println("Найти катет - 1\n" +
  25.                            "Найти гипотенузу - 2");
  26.         int cmd = sc.nextInt();
  27.         switch (cmd){
  28.             case 1:
  29.                 System.out.println("Введите гипотенузу");
  30.                 double gip = sc.nextDouble();
  31.                 System.out.println("Введите катет");
  32.                 double cat = sc.nextDouble();
  33.                 double result = sqrt(pow(gip, 2) - pow(cat, 2));
  34.                 if(Double.isNaN(result)){
  35.                     System.out.println("Треугольник не существует");
  36.                     break;
  37.                 }
  38.                 System.out.println("Второй катет: " + isInt(result));
  39.                 System.out.println("Площадь треугольника: " + isInt(square(gip, cat, result)));
  40.                 break;
  41.             case 2:
  42.                 System.out.println("Введите катет a");
  43.                 double catA = sc.nextDouble();
  44.                 System.out.println("Введите катет b");
  45.                 double catB = sc.nextDouble();
  46.                 double res = sqrt(pow(catA, 2) + pow(catB, 2));
  47.                 if(Double.isNaN(res)){
  48.                     System.out.println("Треугольник не существует");
  49.                     break;
  50.                 }
  51.                 System.out.println("Гипотенуза : " + isInt(res));
  52.                 System.out.println("Площадь треугольника : " + isInt(square(catA, catB, res)));
  53.                 break;
  54.         }
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment