Advertisement
mmayoub

arrays-exercise 05, slide 28

Jul 1st, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. package class170629;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class arrays05 {
  6.  
  7.     public static void main(String[] args) {
  8.         /*
  9.          * Exercise 5, slide 28
  10.          * input : 10 numbers to an array
  11.          * process: add 1 to values in even keys, subtract 1 from 3 multiplications keys
  12.          * output: print all array values
  13.          */
  14.        
  15.         // create a scanner
  16.         Scanner s = new Scanner(System.in);
  17.  
  18.         // define an integer array, size=10
  19.         int[] numbers = new int[10];
  20.  
  21.         System.out.printf("Enter %d integer numbers:\n", numbers.length);
  22.         // get data from user and save it in the array
  23.         for (int i = 0; i < numbers.length; i += 1) {
  24.             numbers[i] = s.nextInt();
  25.  
  26.             numbers[i] += i % 2 == 0 ? 1 : 0; // add 1 to even places
  27.             numbers[i] -= i % 3 == 0 ? 1 : 0; // subtract 1 from 3
  28.                                                 // multiplications places
  29.         }
  30.         s.close();
  31.  
  32.         // print numbers in the array
  33.         //System.out.print("Numbers: ");
  34.         for (int i = 0; i < numbers.length; i += 1) {
  35.             System.out.printf("%d ", numbers[i]);
  36.         }
  37.         System.out.println();
  38.  
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement