Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ArrayMethods{
- public static void main(String[] args){
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter amount of number in your Arrays");// take in array limit from user
- int y = scan.nextInt();
- int firstarray []= new int [y];
- for(int x = 0; x<y; x++){ // initilzed the array
- System.out.println( "Enter element" + ": " + x);
- int a =scan.nextInt();
- firstarray[x]=a;
- }
- System.out.println("Please pick your desire method"); // ask user for method
- System.out.println("1 to Reverse an array");
- System.out.println( "2 to Smallest element of an array");
- System.out.println("3 to Largest element of an array");
- int z = scan.nextInt();
- if(z==1){ // match result with picked methoods
- reverseArray(firstarray, firstarray.length);
- } else if (z==2) {
- smallestValue(firstarray);
- } else if (z==3) {
- largestValue(firstarray);
- }
- System.out.println("Would you like to do another one, 1 for yes , 2 for no");
- int o = scan.nextInt();
- while (i == 1) {
- System.out.println();}
- while(o == 2){
- scan.close();
- }
- public static void reverseArray (int[] arr, int x){ //reverse method
- int [] f = new int[x];
- int j = x;
- for (int i = 0; i < x; i++) {
- f[j - 1] = arr[i];
- j = j - 1;
- }
- for(int k = 0; k < x; k++) {
- System.out.println(f[k]);
- }
- }
- public static void largestValue(int[] arr){ //largest number method
- int max = arr[0];
- for (int i = 1; i < arr.length; i++) {
- if(arr[i] > max) {
- max = arr[i];
- }
- }
- System.out.println("The largest value is: " + max);
- }
- public static void smallestValue(int[] arr){ // smallest number method
- int min = arr[0];
- for(int i = 0; i < arr.length; i++) {
- if(arr[i] < min) {
- min = arr[i];
- }
- }
- System.out.println("The smallest value is: " + min);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement