PDA

View Full Version : iterating an array using a for loop with nested if statements


Eugene123
10-09-2005, 09:31 PM
Ok, so I'm a complete newbie to programming at all. I'm trying to figure out this problem and have spent a considerable ammount of time trying to figure it out. I'm sure it's very easy, but I really don't know where to start. Anyway I need to display the letter grade for each exam using a for loop with nested if statement. This is the best I've come up with which doesn't work.
for(int i = 0; i < 5; i++)

if (i >= 90)
System.out.println("Letter Grade A" );

Here's the array code
public class ArrayDemo
{
public static void main(String[] args)
{ // declare an int array with 5 elements
int testScores[] = new int [5];

// populate the elements
testScores[0] = 75;
testScores[1] = 80;
testScores[2] = 70;
testScores[3] = 85;
testScores[4] = 90;

// display the element contents
System.out.println("test score 1 = " + testScores[0]);
System.out.println("test score 2 = " + testScores[1]);
System.out.println("test score 3 = " + testScores[2]);
System.out.println("test score 4 = " + testScores[3]);
System.out.println("test score 5 = " + testScores[4]);

nikkiH
10-10-2005, 09:27 PM
You didn't try too hard... ;)

(and this is Java, not Javascript. Wrong forum.)


for(int i = 0; i < 5; i++) {
if (testScores[i] >= 90) {
System.out.println("Letter Grade A" );
}
else if (testScores[i] >= 80) {
System.out.println("Letter Grade B" );
}
// keep going
}