使用long时可能会损失精度

时间:2017-12-24 13:22:39

标签: java variables

我还是Java新手,下面给定的程序工作得非常好,直到我将所有int数据类型更改为长数据类型。现在它给我一个精度错误的损失,我不明白为什么?我已将所有数据类型更改为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

1 个答案:

答案 0 :(得分: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值。

     

尝试访问具有长索引值的数组组件会导致编译时错误。