2012-04-02 13 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"のようなメッセージで報告された変更の数が画面に表示されます。配列要素の値を4に変更した場合、配列要素の値を5に変更する方法を教えてください。

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

+2

これはとても宿題です! – dasblinkenlight

+0

'x [i]'は 'int'です。他のifステートメントでどのように使用しますか。 – Jeffrey

答えて

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 
関連する問題