Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class ArrayMethods{
  3. public static void main(String[] args){
  4. Scanner scan = new Scanner(System.in);
  5.  
  6. System.out.println("Enter amount of number in your Arrays");// take in array limit from user
  7. int y = scan.nextInt();
  8. int firstarray []= new int [y];
  9.  
  10. for(int x = 0; x<y; x++){ // initilzed the array
  11. System.out.println( "Enter element" + ": " + x);
  12. int a =scan.nextInt();
  13. firstarray[x]=a;
  14. }
  15.  
  16. System.out.println("Please pick your desire method"); // ask user for method
  17. System.out.println("1 to Reverse an array");
  18. System.out.println( "2 to Smallest element of an array");
  19. System.out.println("3 to Largest element of an array");
  20. int z = scan.nextInt();
  21.  
  22.  
  23. if(z==1){ // match result with picked methoods
  24. reverseArray(firstarray, firstarray.length);
  25. } else if (z==2) {
  26. smallestValue(firstarray);
  27. } else if (z==3) {
  28. largestValue(firstarray);
  29. }
  30. System.out.println("Would you like to do another one, 1 for yes , 2 for no");
  31. int o = scan.nextInt();
  32. while (i == 1) {
  33. System.out.println();}
  34. while(o == 2){
  35.  
  36.  
  37.  
  38.  
  39. scan.close();
  40.  
  41. }
  42.  
  43. public static void reverseArray (int[] arr, int x){ //reverse method
  44. int [] f = new int[x];
  45. int j = x;
  46. for (int i = 0; i < x; i++) {
  47. f[j - 1] = arr[i];
  48. j = j - 1;
  49. }
  50.  
  51. for(int k = 0; k < x; k++) {
  52. System.out.println(f[k]);
  53. }
  54. }
  55.  
  56.  
  57. public static void largestValue(int[] arr){ //largest number method
  58. int max = arr[0];
  59.  
  60. for (int i = 1; i < arr.length; i++) {
  61. if(arr[i] > max) {
  62. max = arr[i];
  63. }
  64. }
  65. System.out.println("The largest value is: " + max);
  66.  
  67. }
  68. public static void smallestValue(int[] arr){ // smallest number method
  69. int min = arr[0];
  70.  
  71.  
  72. for(int i = 0; i < arr.length; i++) {
  73. if(arr[i] < min) {
  74.  
  75. min = arr[i];
  76.  
  77. }
  78. }
  79.  
  80. System.out.println("The smallest value is: " + min);
  81.  
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement