我想檢查長整數是否大於INTEGER.MAX值,但它不工作。這是非常直接的,所以我只是想知道是否有一個Integer對象與我已經完成的比較有問題,否則我不知道問題是什麼。當nextTotal值超過INTEGER.MAX時,它會踢入負數而不是打印錯誤消息。如何比較整數與長?
public Integer initialValue=0;
int amount = Integer.parseInt(amountStr);
System.out.println("Received from client: " + amount);
long nextTotal=amount+initialValue;
if((nextTotal>Integer.MAX_VALUE)|| (nextTotal<Integer.MIN_VALUE)){
System.out.println("Error: Integer has not been added as the total exceeds the Maximum Integer value!");
out.flush();
}
else{
initialValue+=amount;
out.println(initialValue); //server response
System.out.println("Sending total sum of integers to the client:"+initialValue);
out.flush();
}
}
感謝您的幫助 - 我覺得這很簡單! – kellzer