Assignemnt #132: Arrays With For Loops
Code
/// Name: Kelsey Lieberman
/// Period 5
/// Program Name: ArraysWithForLoops
/// File Name: ArraysWithForLoops.java
/// Date Finished: 5/25/2016
import java.util.Scanner;
public class ArraysWithForLoops {
public static void main(String[] args) {
//Create an integer array of 5 elements
int[] myArray = new int[5];
Scanner myScanner = new Scanner(System.in);
//
// IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!
//
// Arrays start at element number 0......... NOT 1
//
// IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!
// Use a for loop to iterate through the array
// Getting input from the user and placing into each array element
for(int i = 0; i < myArray.length; i++) {
System.out.print("Value for item [" + (i+1) + "] = ");
myArray[i] = myScanner.nextInt();
}
System.out.println();
//Print out the elements of the array in order from 0 to 4.
int total = 0;
for(int i = 0; i < myArray.length; i++) {
total = total + myArray[i];
}
System.out.println("The total of the values you have entered for items 1-5 is: " + total);
for(int i = (myArray.length - 1); i >= 0; i--) {
System.out.println("Value for item [" + (i+1) + "] = " + myArray[i]);
}
}
}
Picture of the output