2012-04-02 29 views
1

数组x={2, 3, 4, 4, -5, 4, 6, 2}结果数组为x={2, 3, 5, 5, -5, 5, 6, 2},并在屏幕上打印报告的变化数量为"The number of changes is 3"如果将数组元素的值更改为5,如果它等于4并通知更改数目,该如何更改?

public class anArray { 
    public static void main(String[]args) { 
     int [] x = {2, 3, 4, 4, -5, 4, 6, 2}; 

     for (int i=0; i<x.length; i++) { 
      System.out.println(x[i]); 
     } //currently only I am able to print whole array element 
    } 
} 
+5

我会看看到[if语句(http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html)如果我是你。 – Jeffrey 2012-04-02 22:19:05

+2

这是作业! – dasblinkenlight 2012-04-02 22:22:21

+0

'x [i]'只是一个'int'。使用它如何与任何其他if语句一起使用。 – Jeffrey 2012-04-02 22:25:22

回答

5
public class anArray { 
    public static void main(String[]args) { 
     int [] x = {2, 3, 4, 4, -5, 4, 6, 2}; 
     int count =0; 
     for (int i=0; i<x.length; i++) { 
      if (x[i] == 4) { 
       count++; 
       x[i]=5; 
      } 
     } 
     system.out.println("...."+count); 
    } //main() 
} //anArray