Assignment #132 and Arrays with For Loops
Code
/// Name: Brendan Baird
/// Period: 6
/// Program Name: Arrays with For Loops
/// File Name: ArraysWithForLoops.java
/// Date Finished: 5/9/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 += myArray[i];
}
System.out.println(total);
for(int i = myArray.length - 1; i > -1; i--) {
System.out.println("Value for item [" + (i+1) + "] = " + myArray[i]);
}
}
}
Picture of the output