저는 여전히 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
@JohnColeman Arrays는 int를 필요로합니다. 그들에게 길게주는 것은 너무 많은 데이터이므로 컴파일러가 행복해 지도록 int로 캐스팅해야합니다. 정밀도가 상실 될 수 있으려면 윗부분의 절반을 버리십시오. – phflack
https://stackoverflow.com/questions/14571557/create-an-array-of-long –
https://stackoverflow.com/questions/39586612/confused 어레이의 최대 크기를 나타낼 수 있다고 덧붙여 야합니다. (참고 자료) –