Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package class170629;
- import java.util.Scanner;
- public class arrays08 {
- public static void main(String[] args) {
- /*
- * Exercise 8, slide 29
- * input : 10 numbers , save every 5 to an array
- * process: save to each key in the a third array the sum of the values from the same keys in the two other arrays
- * output: the same keys that has the same values
- */
- // size of each array
- final int SIZE = 5;
- // create a scanner
- Scanner s = new Scanner(System.in);
- // define an integer arrays
- int[] numbers1 = new int[SIZE];
- int[] numbers2 = new int[SIZE];
- int[] sum = new int[SIZE];
- System.out.printf("Enter %d integer numbers to first array:\n",
- numbers1.length);
- // get data from user and save it in the array
- for (int i = 0; i < numbers1.length; i += 1) {
- numbers1[i] = s.nextInt();
- }
- System.out.printf("Enter %d integer numbers to seconed array:\n",
- numbers2.length);
- // get data from user and save it in the array
- for (int i = 0; i < numbers2.length; i += 1) {
- numbers2[i] = s.nextInt();
- }
- s.close();
- // add and save to results array, then print it
- for (int i = 0; i < sum.length; i += 1) {
- // save sum to results array
- sum[i] = numbers1[i] + numbers2[i];
- // print results
- System.out.printf("%d ", sum[i]);
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement