Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static void main_input(long[] array){
  6. Scanner in =new Scanner(System.in);
  7. for(int i=0;i<array.length;i++){
  8. array[i]=in.nextLong();
  9. }
  10. }
  11. public static void auto_input(long[] array){
  12. int n=array.length;
  13. for (int i=0;i<n;i++){
  14. array[i]=(long) (Math.random()*999);
  15. }
  16. }
  17. public static void Left_to_right_output(long[] array){
  18. int n = array.length;
  19. System.out.print("Массив:");
  20. for(int i=0;i<n;i++){
  21. if(i<n-1){
  22. System.out.print(array[i]+",");
  23. }else{
  24. System.out.print(array[i]);
  25. System.out.println();
  26. }
  27. }
  28. }
  29. public static boolean Check_value(long[] array,long value){
  30. for (long x:array){
  31. if(x==value){
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. public static long[] Bubble_Sort(long[] array){
  38. int n=array.length;
  39. for(int i = n-1 ; i > 0 ; i--) {
  40. for (int j = 0; j < i; j++) {
  41.  
  42. if (array[j] > array[j + 1]) {
  43. long tmp = array[j];
  44. array[j] = array[j + 1];
  45. array[j + 1] = tmp;
  46. }
  47. }
  48.  
  49. }
  50. return array;
  51. }
  52. public static void Right_to_left_output(long[] array){
  53. int n=array.length;
  54. System.out.print("Массив:");
  55. for(int i=n-1;i>=0;--i){
  56. if(i!=0){
  57. System.out.print(array[i]+",");
  58. }else{
  59. System.out.print(array[i]);
  60. System.out.println();
  61. }
  62. }
  63. }
  64. public static void main(String[] args) {
  65. System.out.println("Укажите размер массива");
  66. Scanner in = new Scanner(System.in);
  67. int n = in.nextInt();
  68. long array[] = new long[n];
  69. boolean k = true;
  70. while (k) {
  71. System.out.println("Меню: ");
  72. System.out.println("1-Ручной ввод ");
  73. System.out.println("2-Автоматический ввод");
  74. System.out.println("3-Вывод слева на право");
  75. System.out.println("4-Вывод справа налево");
  76. System.out.println("5-Проверить есть ли указанное значение в массиве");
  77. System.out.println("6-Отсортировать массив методом пузырька");
  78. System.out.println("0-Выход");
  79. String command = in.next();
  80. if (command.equals("1")) {
  81. main_input(array);
  82. }
  83. if (command.equals("2")) {
  84. auto_input(array);
  85. }
  86. if (command.equals("3")) {
  87. Left_to_right_output(array);
  88. in.nextLine();
  89. in.nextLine();
  90. }
  91. if (command.equals("4")) {
  92. Right_to_left_output(array);
  93. in.nextLine();
  94. in.nextLine();
  95. }
  96. if (command.equals("5")) {
  97. System.out.println("Укажите значение:");
  98. long value = in.nextLong();
  99. if (Check_value(array,value)){
  100. System.out.println("Такое значение есть ");
  101. }else{
  102. System.out.println("Значения нет ");
  103. }
  104. in.nextLine();
  105. in.nextLine();
  106. }
  107. if (command.equals("6")) {
  108. System.out.println("Отсортированный массив: ");
  109. Left_to_right_output(Bubble_Sort(array));
  110. in.nextLine();
  111. in.nextLine();
  112. }
  113. if (command.equals("0")) {
  114. k=false;
  115. }
  116. }
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement