2017-12-24 1 views
2

저는 여전히 Java에 익숙하지 않습니다. 아래의 주어진 프로그램은 모든 int 데이터 유형을 long 데이터 유형으로 변경하기 전까지는 아무런 문제가 없습니다. 지금 그것은 나에게 정밀도 오류의 손실을주고 있으며 나는 왜 이해가 안되니? 모든 데이터 형식을 오래 변경했지만 아직이 오류가 발생합니다. 도와주세요. 여기 long을 사용할 때 정밀도가 저하 될 수 있습니다.

public class Solution { 

    public static void main(String[] args) { 
     long i,j,great=0,gpos=0,k=1,temp=0; 
     Scanner in = new Scanner(System.in); 

     long n = in.nextLong(); 
     long[] scores = new long[n]; 

     for(long scores_i = 0; scores_i < n; scores_i++){ 
      scores[scores_i] = in.nextLong(); 
     } 

     long m = in.nextLong(); 
     long[] alice = new long[m]; 

     for(long alice_i = 0; alice_i < m; alice_i++){ 
      alice[alice_i] = in.nextLong(); 
     } 

     long rank[] = new long[n]; 

     for(j=0;j<n;j++){ 
      great = 0; 
      for(i=0;i<n;i++){ 
       if(rank[i]!=0){ 
        continue; 
       } 
       if(great<scores[i]){ 
        great=scores[i]; 
        gpos=i; 
       } 
      } 

      if(temp==great){ 
       k--; 
       rank[gpos]=k; 
       k++; 
      } 
      else{ 
       rank[gpos]=k; 
       temp=great; 
       k++; 
      } 
     } 

     for(i=0;i<m;i++){ 
      for(j=0;j<n;j++){ 
       if(alice[i]>scores[j]){ 
        System.out.println(rank[j]); 
        break; 
       }else if(alice[i]==scores[j]){ 
        System.out.println(rank[j]); 
        break; 
       }else if(j==(n-1)){ 
        System.out.println(rank[j]+1); 
        break; 
       } 
      } 

     } 

     in.close(); 
    } 
} 

내가 점점 계속 오류 보고서입니다 :

Solution.java:11: error: possible loss of precision 
     long[] scores = new long[n]; 
           ^
    required: int 
    found: long 
Solution.java:14: error: possible loss of precision 
      scores[scores_i] = in.nextLong(); 
       ^
    required: int 
    found: long 
Solution.java:18: error: possible loss of precision 
     long[] alice = new long[m]; 
           ^
    required: int 
    found: long 
Solution.java:21: error: possible loss of precision 
      alice[alice_i] = in.nextLong(); 
       ^
    required: int 
    found: long 
Solution.java:24: error: possible loss of precision 
     long rank[] = new long[n]; 
          ^
    required: int 
    found: long 
Solution.java:29: error: possible loss of precision 
           if(rank[i]!=0){ 
             ^
    required: int 
    found: long 
Solution.java:32: error: possible loss of precision 
           if(great<scores[i]){ 
               ^
    required: int 
    found: long 
Solution.java:33: error: possible loss of precision 
             great=scores[i]; 
                ^
    required: int 
    found: long 
Solution.java:40: error: possible loss of precision 
           rank[gpos]=k; 
            ^
    required: int 
    found: long 
Solution.java:44: error: possible loss of precision 
           rank[gpos]=k; 
            ^
    required: int 
    found: long 
Solution.java:52: error: possible loss of precision 
           if(alice[i]>scores[j]){ 
             ^
    required: int 
    found: long 
Solution.java:52: error: possible loss of precision 
           if(alice[i]>scores[j]){ 
               ^
    required: int 
    found: long 
Solution.java:53: error: possible loss of precision 
             System.out.println(rank[j]); 
                   ^
    required: int 
    found: long 
Solution.java:55: error: possible loss of precision 
           }else if(alice[i]==scores[j]){ 
              ^
    required: int 
    found: long 
Solution.java:55: error: possible loss of precision 
           }else if(alice[i]==scores[j]){ 
                 ^
    required: int 
    found: long 
Solution.java:56: error: possible loss of precision 
             System.out.println(rank[j]); 
                   ^
    required: int 
    found: long 
Solution.java:59: error: possible loss of precision 
             System.out.println(rank[j]+1); 
                   ^
    required: int 
    found: long 
17 errors 
+0

@JohnColeman Arrays는 int를 필요로합니다. 그들에게 길게주는 것은 너무 많은 데이터이므로 컴파일러가 행복해 지도록 int로 캐스팅해야합니다. 정밀도가 상실 될 수 있으려면 윗부분의 절반을 버리십시오. – phflack

+0

https://stackoverflow.com/questions/14571557/create-an-array-of-long –

+0

https://stackoverflow.com/questions/39586612/confused 어레이의 최대 크기를 나타낼 수 있다고 덧붙여 야합니다. (참고 자료) –

답변

2

당신은 배열 인덱스로 long를 사용할 수 있으며, 배열의 가능한 최대 길이는 Integer.MAX_VALUE입니다.

따라서 배열의 인덱스로서 사용되는 변수 int 같아야

for(int alice_i = 0; alice_i < m; alice_i++){ 
    alice[alice_i] = in.nextLong(); 
} 

동일한는 어레이 인덱스로서 사용하는 모든 변수 간다.

JLS 10.4. Array Access 참조 :

INT에 의해 색인해야이 값 배열; short, byte 또는 char 값은 단항 숫자 승격 (§5.6.1)을 받고 int 값이되기 때문에 인덱스 값으로 사용할 수도 있습니다.

인덱스 값이 긴 배열 구성 요소에 액세스하려고하면 컴파일 타임 오류가 발생합니다.

+1

어레이의 최대 크기를 나타낼 수 있다고 덧붙여 야합니다. 의해 int – phflack

+0

그래서 어떻게 int 200000 요소를 사용하지 않고 배열을 얻을합니까 ?? –

+0

@OmkarLubal 왜'int'를 쓰지 않습니까? – Eran

관련 문제