Sanctus Secretum
Sanctus Secretum

Reputation: 71

if else statement inside a for loop?

On the final part of a project i'm doing for school, I am supposed to use a if-else statement inside a for loop, but I have no idea how to do this. I could just use a huge lot of if-else statements to do the same thing, but I dont think my teacher would appreciate it.

Here are the instructions for the final part of the homework...

Compute the Grade (A, B, C, D or F) and store in another Array8 called grades using if-else loop and the following cutoff inside a for-loop to assign grades.

1.    Average                        Grade
2.    >= 90                          A
3.    >=80 and <90                   B
4.    >=70 and <80                   C
5.    >=60 and <70                   D
6.    <60                            F

This is my code so far...

public class Proj5 {
    public static void main (String[] args) {
        String[] Array1= {new String("Adam"),new String("Smith"),new String("Jones"),new String("Becky"),new String("Taylor")};
        Integer[] Array2={new Integer(90),new Integer(89),new Integer(86),new Integer(76),new Integer(95)};
        Integer[] Array3={new Integer(92),new Integer(79),new Integer(85),new Integer(90),new Integer(87)};
        Integer[] Array4={new Integer(93),new Integer(80),new Integer(90),new Integer(87),new Integer(92)};
        Integer[] Array5={new Integer(90),new Integer(77),new Integer(86),new Integer(92),new Integer(89)};

        double av1 = (((Array2[0]+Array3[0]+Array4[0])/3));
        double av2 = (((Array2[1]+Array3[1]+Array4[1])/3));
        double av3 = (((Array2[2]+Array3[2]+Array4[2])/3));
        double av4 = (((Array2[3]+Array3[3]+Array4[3])/3));
        double av5 = (((Array2[4]+Array3[4]+Array4[4])/3));

        double[] Array6 = {(av1),(av2),(av3),(av4),(av5)};

        double avf1 = (av1*.30)+(Array5[0]*.7);
        double avf2 = (av2*.30)+(Array5[1]*.7);
        double avf3 = (av3*.30)+(Array5[2]*.7);
        double avf4 = (av4*.30)+(Array5[3]*.7);
        double avf5 = (av5*.30)+(Array5[4]*.7);

        double[] Array7 = {(avf1),(avf2),(avf3),(avf4),(avf5)};

        System.out.println("Report for Spring Semester 2009"+
        "\n-------------------------------------------");
        System.out.println("Name Test1 Test2 Test3 Final Average Grade");

        for (int column = 0; column<Array1.length; column++){
            System.out.printf("%s ", Array1[column]);
            System.out.printf("%s   ", Array2[column]);
            System.out.printf("%s    ", Array3[column]);
            System.out.printf("%s    ", Array4[column]);
            System.out.printf("%s    ", Array5[column]);
            System.out.printf("%s    ",  Array7[column]);
            System.out.println(); //start new line of output
        } 
    }
}

Upvotes: 2

Views: 146444

Answers (4)

Itchy Nekotorych
Itchy Nekotorych

Reputation: 992

char[] Array8 = new char[5];

for (int i = 0; i < Array8.length;i++ ) {
    if (Array6[i] >= 90)
        Array8[i] = 'A';
    else if (Array6[i] >= 80)
        Array8[i] = 'B';
    else if (Array6[i] >= 70)
        Array8[i] = 'C';
    else if (Array6[i] >= 60)
        Array8[i] = 'D';
    else
        Array8[i] = 'F';
}

Upvotes: 4

David Harkness
David Harkness

Reputation: 36522

It seems the assignment is asking you to write an if/else chain inside a for loop.

for (...) {
    if (average >= 90)
        grade = 'A';
    else if (average >= 80)
        grade = 'B';
    else if (average >= 70)
        grade = 'C';
    else if (average >= 60)
        grade = 'D';
    else
        grade = 'F';
}

Upvotes: 2

Luke
Luke

Reputation: 5076

If else?

If he means he needs it in the loop then I'm guessing that it's an if statement and an else statement inside the for loop. Or it could possibly be a else if loop, pretty much just the same except the else has extra conditions. Something like this:

for(int i = 0; i < 10; i++) {
    if(i < 5) {
        System.out.println("I am smaller than five");
    }
    else /*Or perhaps if an else if, 'else if(i > 7){System.out.println('I am bigger than seven');}'*/{
        System.out.println("I am bigger than five");
    }
}

Upvotes: 2

talnicolas
talnicolas

Reputation: 14053

if/else is not a loop. If you have to use an if/else statement in a for loop, just do it:

for(int i = 0; i < Array8.length; i++) {
    if(...) {
        ...
    } else {
        ...
    }
}

Upvotes: 8

Related Questions