简体   繁体   English

是否在for循环内声明?

[英]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. 在我要上学的项目的最后一部分,我应该在for循环中使用if-else语句,但是我不知道该怎么做。 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. 我可以使用大量的if-else语句来做同样的事情,但是我不认为我的老师会喜欢它。

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. 计算等级(A,B,C,D或F),并使用if-else循环和for循环内的以下截止值将等级存储在另一个称为等级的Array8中,以分配等级。

 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
        } 
    }
}

if/else is not a loop. if/else不是循环。 If you have to use an if/else statement in a for loop, just do it: 如果必须在for循环中使用if/else语句,只需执行以下操作:

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

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. 如果他表示他需要在循环中使用它,那么我猜这是for循环中的if语句和else语句。 Or it could possibly be a else if loop, pretty much just the same except the else has extra conditions. 或者它可能是else if循环,除了else具有额外条件外,其他几乎一样。 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");
    }
}

It seems the assignment is asking you to write an if/else chain inside a for loop. 似乎作业要求您在for循环内编写if/else

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';
}
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';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM