0

Basically I have an array with a length of 6. This array is used in a simple for loop. Within the for loop I have an if statement. The problem is, if the condition in the if statement is met, all of the contents off the if statement are obviously performed. This is not what I want.

I need if, for example, the 2nd and 4th element of the array only pass the condition then only for "arrayp2" and "arrayp4" to be performed.

Any help would be amazing. Thanks in advance!

for (int i = 0; i < array.length; i++)
{
    if (array[i] >= 100)
    {
        arrayp1.someMethod(Action);
        arrayp2.someMethod(Action);                 
        arrayp3.someMethod(Action);             
        arrayp4.someMethod(Action);             
        arrayp5.someMethod(Action);                     
        arrayp6.someMethod(Action);
    }
}
4

3 に答える 3

0

switch ステートメントが必要だと思います。

for (int i = 1; i <= array.length; i++)
{
    switch(i){
         case 1: if (array[i - 1] >= 100) arrayp1.someMethod(Action); break; 
         case 2: if (array[i - 1] >= 100) arrayp2.someMethod(Action); break; 
         case 3: if (array[i - 1] >= 100) arrayp3.someMethod(Action); break; 
         case 4: if (array[i - 1] >= 100) arrayp4.someMethod(Action); break; 
         case 5: if (array[i - 1] >= 100) arrayp5.someMethod(Action); break; 
         case 6: if (array[i - 1] >= 100) arrayp6.someMethod(Action); break; 
    }
}
于 2013-11-08T09:50:37.487 に答える