Advertisement
Luchezar16215

Domashno3

Oct 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. new paste
  2.  
  3.  
  4. IntroductionJava9_9
  5.  UKTC162   OCT 10TH, 2019  17  NEVER
  6.  
  7. rawdownloadreport1.69 KB
  8. import java.util.Scanner;
  9.  
  10. public class IntroductionJava9_9 {
  11.  
  12.     public static void main(String[] args) {
  13.  
  14.         Scanner sc = new Scanner(System.in);
  15.  
  16.         System.out.println("Choose 1 for reversed, 2 for average and 3 for equation:");
  17.         int choice = Integer.parseInt(sc.nextLine());
  18.         if (choice == 1) {
  19.             System.out.println("Type your number:");
  20.             int number = Integer.parseInt(sc.nextLine());
  21.             System.out.println(reversed(number));
  22.         } else if (choice == 2) {
  23.             System.out.println("Type length of array:");
  24.             int lengthArr = Integer.parseInt(sc.nextLine());
  25.             int[] arr = new int[lengthArr];
  26.             if (lengthArr == 0) {
  27.                 System.out.println("NO ELEMENTS IN ARRAY!");
  28.             } else {
  29.                 System.out.println("Type elements of array:");
  30.                 for (int i = 0; i < arr.length; i++) {
  31.                     arr[i] = Integer.parseInt(sc.nextLine());
  32.                 }
  33.                 System.out.println(average(arr));
  34.             }
  35.         } else if (choice == 3) {
  36.             System.out.println("Type 'a' and 'b':");
  37.             int a = Integer.parseInt(sc.nextLine());
  38.             if (a == 0) {
  39.                 System.out.println("'A' CAN'T BE 0");
  40.             }
  41.             int b = Integer.parseInt(sc.nextLine());
  42.             System.out.println(equation(a, b));
  43.         } else {
  44.             System.out.println("1, 2 OR 3 NOT DETECTED");
  45.         }
  46.     }
  47.  
  48.     public static int reversed(int a) {
  49.         int reversed = 0;
  50.         while (a != 0) {
  51.             int digit = a % 10;
  52.             reversed = reversed * 10 + digit;
  53.             a /= 10;
  54.         }
  55.         return reversed;
  56.  
  57.     }
  58.  
  59.     public static float average(int[] array) {
  60.         float sum = 0;
  61.         float average = 0;
  62.         for (int i = 0; i < array.length; i++) {
  63.             sum = sum + array[i];
  64.             average = sum / array.length;
  65.             ;
  66.         }
  67.         return average;
  68.     }
  69.  
  70.     public static float equation(float a, float b) {
  71.         return -b / a;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement