2016-10-26 2 views
1

어떤 이유로 든 내가 만든 for 루프 외부에서 배열 값에 액세스 할 수 없습니다. 배열을 로컬로 선언했습니다. 하지만 사용자가 값을 입력해야 할 때 어떤 이유에서든 바인딩 된 값은 계속 0을 제공합니다. for 루프 내부의 배열로부터 정보를 얻는 방법이 "일반화"되어 다른 함수에서도 사용할 수 있습니다. 당신은 당신은 배열의 각 요소를 초기화하지 않는 for-loop을 강화 사용하고 있기 때문에입니다작동 할 때에도 배열 값에 액세스 할 수 없습니다.

import java.util.*; 

public class Array { 

     public static void main(String[] args) throws IndexOutOfBoundsException { 

     int[] array = new int[100]; 
     try{ 
     for(int i: array){ 
      array[i] = (int)(Math.random()*100); 
      System.out.println(array[i]); 
      } 
      Scanner input = new Scanner(System.in); 
      System.out.println("Please enter an index for which you would like to see: "); 
      int index = input.nextInt(); 
      System.out.println(array[index]); 

     }catch(IndexOutOfBoundsException ex){ 
      System.out.println("Please enter in an index thta is not out of bounds"); 
     }finally{System.out.println("--------------------------");} 
    } 
} 

답변

0

, 값을 얻을 수있는 유일한 요소는 첫 번째 요소 제로이다. 배열을 선언 할 때 모든 값은 0으로 설정됩니다. 따라서 향상된 for-loop에서는 array[0]의 값을 설정하고 있기 때문에 그 결과로 0이 출력됩니다.

이와 강화 된 for-loop 교체 :

 int[] array = new int[100]; 
     try{ 
     for(int i=0;i<array.length;i++){ 
      array[i] = (int)(Math.random()*100); 

      System.out.println(array[i]); 
      } 
      Scanner input = new Scanner(System.in); 
      System.out.println("Please enter an index for which you would like to see: "); 
      int index = input.nextInt(); 
      System.out.println(array[index]); 

     }catch(IndexOutOfBoundsException ex){ 
      System.out.println("Please enter in an index thta is not out of bounds"); 
     }finally{ 
      System.out.println("--------------------------"); 
     } 
+0

그럼 머리에 얼굴을 손바닥 이잖아. 그러나 논리 현명한 shouldnt "강화"에 대한 일반적인 기능을 가지고 루프에 대한 일반, 그리고 몇 가지? – Dcdw51

+0

@ Dcdw51 내 대답에 설명을 추가했습니다. 체크 아웃하십시오. 향상된 for 루프는 단지 반복을위한 것이지 인덱스 값이 할당 변수 배열에 대해 증가하지 않기 때문에 일반적인 배열 할당을위한 것이 아닙니다 –

2

루프이를 사용하여 시도하고 작동합니다을 :

for(int i=0;i<array.length;i++){ 
      array[i] = (int)(Math.random()*100); 
      System.out.println(array[i]); 
     } 

코드에서 문제가 있었다 :

당신이 for(int i: array) 배열의 내용을 배열 [0]에서 배열 [99]로 i로 복사하는 것을 지시하는 것입니다. 다른 값이 없으면 항상 0이됩니다. 세트. 루프 강화 은

이해 적은 코드의 반복에 사용되는 방법 향상된 루프 작품, 양해 아래의 루프 시도 :

int[] array = new int[100]; 
     array[0]=1;array[1]=2; 

     for(int i: array){ 
      // array[i] = (int)(Math.random()*100); 
      System.out.println(i); 
     } 

이 코드를 실행하면, 제 1 및 제 2을 요소는 1과 2를 인쇄합니다.

희망 사항을 지금 삭제하십시오.

0

배열을 선언 할 때 100 개의 인덱스를 지정합니다. 그들은 자동으로 모두 0이됩니다. 각 루프에 대해 배열의 0 번째 인덱스에 임의의 숫자를 할당하면 나머지는 모두 0으로 유지됩니다. 실제로 배열의 i 번째 요소에 액세스하지는 않습니다!

이 코드를 수정해야합니다 :

public static void main(String[] args) { 
    int[] array = new int[100]; 
     try{ 
     for(int i=0; i<array.length; i++){ 
      array[i] = (int)(Math.random()*100); 
      System.out.println(array[i]); 
      } 

      Scanner input = new Scanner(System.in); 
      System.out.println("Please enter an index for which you would like to see: "); 
      int index = input.nextInt(); 
      System.out.println(array[index]); 

     }catch(IndexOutOfBoundsException ex){ 
      System.out.println("Please enter in an index thta is not out of bounds"); 
     }finally{System.out.println("--------------------------");} 

     } 
} 
관련 문제