Advertisement
mmayoub

arrays-exercise 07, slide 29

Jul 1st, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. package class170629;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class arrays07 {
  6.  
  7.     public static void main(String[] args) {
  8.         /*
  9.          * Exercise 7, slide 29
  10.          * input : 10 numbers , save every 5 to an array
  11.          * output: the same keys that has the same values
  12.          */
  13.        
  14.         // size of each array
  15.         final int SIZE = 5;
  16.  
  17.         // create a scanner
  18.         Scanner s = new Scanner(System.in);
  19.  
  20.         // define an integer arrays
  21.         int[] numbers1 = new int[SIZE];
  22.         int[] numbers2 = new int[SIZE];
  23.  
  24.         System.out.printf("Enter %d integer numbers to first array:\n",
  25.                 numbers1.length);
  26.         // get data from user and save it in the array
  27.         for (int i = 0; i < numbers1.length; i += 1) {
  28.             numbers1[i] = s.nextInt();
  29.         }
  30.         System.out.printf("Enter %d integer numbers to seconed array:\n",
  31.                 numbers2.length);
  32.         // get data from user and save it in the array
  33.         for (int i = 0; i < numbers2.length; i += 1) {
  34.             numbers2[i] = s.nextInt();
  35.         }
  36.         s.close();
  37.  
  38.         // check numbers at the same position, if equal then print the position
  39.         for (int i = 0; i < numbers1.length; i += 1) {
  40.             if (numbers1[i]  == numbers2[i])
  41.                 System.out.printf("%d is in place %d in both arrays\n", numbers1[i], i);
  42.         }
  43.         System.out.println();
  44.  
  45.     }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement