Advertisement
KechevD

HW1

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