Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package class170629;
- import java.util.Scanner;
- public class arrays05 {
- public static void main(String[] args) {
- /*
- * Exercise 5, slide 28
- * input : 10 numbers to an array
- * process: add 1 to values in even keys, subtract 1 from 3 multiplications keys
- * output: print all array values
- */
- // create a scanner
- Scanner s = new Scanner(System.in);
- // define an integer array, size=10
- int[] numbers = new int[10];
- System.out.printf("Enter %d integer numbers:\n", numbers.length);
- // get data from user and save it in the array
- for (int i = 0; i < numbers.length; i += 1) {
- numbers[i] = s.nextInt();
- numbers[i] += i % 2 == 0 ? 1 : 0; // add 1 to even places
- numbers[i] -= i % 3 == 0 ? 1 : 0; // subtract 1 from 3
- // multiplications places
- }
- s.close();
- // print numbers in the array
- //System.out.print("Numbers: ");
- for (int i = 0; i < numbers.length; i += 1) {
- System.out.printf("%d ", numbers[i]);
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement