2012-04-02 1 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

내가에 좀 걸릴 것 [경우 문 (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 
관련 문제