Tutorial05 - We will take care of this in the next step...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Tutorial #5: Array Basics O P T I O N A L Introduction: This tutorial is recommended if you have limited experience using arrays. 1. Enter the following code into a program and execute it. It should display the three array values. This is an example of “hardcoding” the array values in the program in advance. int[] grades = {95, 87, 90}; for (int i = 0; i < grades.length; i++) System.out.println(grades[i]); 2. Change your program to get the grades from the user as shown below. Don’t forget to add “import java.util.Scanner” at the top of your program. Run the program. Notice that the values are not printed out.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: We will take care of this in the next step. Scanner scanner = new Scanner(System.in); System.out.print("How many grades are there? "); int size = scanner.nextInt(); int grades; grades = new int[size]; for (int i = 0; i < grades.length; i++) { System.out.print("Enter grade #" + i + ": "); grades[i] = scanner.nextInt(); } 3. Add the following code to print the array values in reverse order. Notice that the variable “i” is initialized to the highest index value which is the length – 1. for (int i = grades.length - 1; i >= 0; i--) System.out.println("Grade #" + i + ": " + grades[i]);...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online