Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- /**
- * Name:
- */
- public class ArrayPractice
- {
- public static void main(String[] args)
- {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter the first number: ");
- int start = scanner.nextInt();
- int finish = start;
- while (finish <= start)
- {
- System.out.print("Enter the last number (must be after first number): ");
- finish = scanner.nextInt();
- }
- // initialize an array to contain the numbers 'start' through 'finish', inclusive
- int[] numbers = buildArray(start, finish);
- // print the message passed in, then all the numbers in the array
- System.out.printf(Arrays.toString(numbers));
- // print the sum and average of the numbers
- printSumAndAverage(numbers);
- // Bonus:
- // add 1 to all odd numbers
- addOneToOddNumbers(numbers);
- System.out.printf(Arrays.toString(numbers));
- reverse(numbers);
- System.out.println("");
- System.out.printf(Arrays.toString(numbers));
- }
- public static int[] buildArray(int start, int finish)
- {
- int lengte=(finish-start)+1;
- int array[]=new int[lengte];
- int teller=0;
- for (int i=start;i<finish+1;i++)
- {
- array[teller]=i;
- ++teller;
- }
- return array;
- }
- /**
- * Method that prints the SUM and the AVERAGE of the numbers in the array
- */
- public static void printSumAndAverage(int[] array)
- {
- int som=0;
- int gemiddelde=0;
- for(int i=0; i<array.length;i++)
- {
- som += array[i];
- }
- gemiddelde = som/array.length;
- System.out.printf("%nDe som is %s%nHet gemiddelde is %s%n",som,gemiddelde);
- }
- // Start with this main() method. Do not change it!
- public static void addOneToOddNumbers(int[]array)
- {
- for (int i=0; i<array.length;i++)
- {
- if(array[i]%2==0)
- {++array[i];}
- }
- }
- public static void reverse (int[]arr)
- { int temp=0;
- int l = arr.length-1;
- for(int i=0;i<l/2;i++)
- {
- temp=arr[l];
- arr[l]=arr[i];
- arr[i]=temp;
- l--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement