Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. Scanner input = new Scanner(System.in);
  2.  
  3. System.out.print("Enter the size of the array: ");
  4. int numbers = input.nextInt();
  5.  
  6. // Declare an array called numbers with a size of 10
  7. int[] numbers1 = new int[numbers];
  8.  
  9. insertRandomNumbers(numbers1);
  10.  
  11. // Print size of numbers
  12. System.out.println("Initial Array: ");
  13. for (int i = 0; i < numbers1.length; i++) {
  14. System.out.print(numbers1[i] + " ");
  15. }
  16. System.out.println("");
  17.  
  18.  
  19. //Print First and Last Elements
  20. System.out.println();
  21. System.out.println("First and Last Elements");
  22.  
  23. int [] lastStep = lastStep(numbers1);
  24. for (int i = 0; i < lastStep.length; i++) {
  25. System.out.print(lastStep[i] + ", ");
  26. }
  27.  
  28.  
  29.  
  30.  
  31. } // end main
  32.  
  33. public static int[] lastStep(int[] numbers1) {
  34.  
  35. //to get first
  36. int [] firstElement= numbers1.get(0);
  37.  
  38. //last number
  39. int [] lastElement= numbers1.get(numbers1.size()-1);
  40.  
  41. }
  42. return lastStep;
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. public static void insertRandomNumbers(int[] x) {
  53. for (int i = 0; i < x.length; i++) {
  54. x[i] = random();
  55. // System.out.print(x[i] + " ");
  56. }
  57. // System.out.println();
  58. }
  59.  
  60. public static int random() {
  61.  
  62. int r = 0 + (int) (Math.random() * (101 - 0)) + 0;
  63. return r;
  64.  
  65. }
  66.  
  67. //to get first
  68. int [] firstElement= numbers1.array[0];
  69.  
  70. //last number
  71. int [] lastElement= numbers1.array[numbers1.size()-1];
  72.  
  73. }
  74. return lastStep;
  75.  
  76. public static string firstAndLast(int[] numberArray)
  77. {
  78. return numberArray[0] + ", " + numberArray[numberArray.length - 1];
  79. }
  80.  
  81. System.out.println(firstAndLast(numbers1));
  82.  
  83. var i = int[] //Array of random numbers
  84. var x = int[y]
  85. /*Then get `x` instead of retrieving the numbers from the whole array.*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement