Java 为什么Integer.MAX_值为+;1小于Integer.MAX_值?
我只是尝试将数字从1添加到Integer.MAX_值,但在输出中我什么也得不到。该程序介于两者之间。下面是我创建的类Java 为什么Integer.MAX_值为+;1小于Integer.MAX_值?,java,Java,我只是尝试将数字从1添加到Integer.MAX_值,但在输出中我什么也得不到。该程序介于两者之间。下面是我创建的类 public class Test { public static void main(String args[]) { long sum = 0; int start_value = 1; long end_value = Integer.MAX_VALUE; while(start_value <
public class Test {
public static void main(String args[]) {
long sum = 0;
int start_value = 1;
long end_value = Integer.MAX_VALUE;
while(start_value <= end_value){
sum += start_value;
start_value++;
}
System.out.println(sum);
}
}
公共类测试{
公共静态void main(字符串参数[]){
长和=0;
int start_值=1;
长端_值=整数。最大_值;
while(start_value这是由于一种称为整数溢出的原因。当您将1
添加到MAX_value
时,如果您使用有符号整数,您将获得MIN_value
,如果使用无符号整数,则获得0
简单解释一下,例如,当你将1
添加到99
时,你必须携带1
两次,以第三位结束:100
。但是如果你最多只允许两位,那么你携带一位两次,以00
结束。在计算机中,位数是有限的(二进制数字)允许,通常为32或64
您可以在此处阅读更多信息:
这是因为一种叫做整数溢出的东西。当您将1
添加到MAX\u值
时,如果您使用有符号整数,您将得到MIN\u值,如果使用无符号整数,则得到0
简单解释一下,例如,当你将1
添加到99
时,你必须携带1
两次,以第三位结束:100
。但是如果你最多只允许两位,那么你携带一位两次,以00
结束。在计算机中,位数是有限的(二进制数字)允许,通常为32或64
您可以在此处阅读更多信息:
它永远不应该完成,因为您有一个无限循环
你的循环是有效的
while(start_value <= Integer.MAX_VALUE) {
您可以更改循环以执行所需操作
int start_value = 0;
while(start_value < end_value) {
start_value++;
sum += startValue;
}
这将在i
溢出时停止。它永远不会完成,因为您有一个无限循环
你的循环是有效的
while(start_value <= Integer.MAX_VALUE) {
您可以更改循环以执行所需操作
int start_value = 0;
while(start_value < end_value) {
start_value++;
sum += startValue;
}
当i
溢出时,此操作将停止。除其他答案外,如果您因无法达到最大整数值而陷入无限循环,您可能需要在循环中添加一个打印,以便查看进度:
public class Test {
public static void main(String args[]) {
long sum = 0;
int start_value = 1;
int end_value = Integer.MAX_VALUE - 1;//Just in case
while(start_value <= end_value){
sum += start_value;
start_value++;
//Print every 100 loops, change this if it prints too often
if (start_value % 100 == 0){
System.out.println("integer at: " + start_value + ", sum: " + sum);
}
}
System.out.println(sum + Integer.MAX_VALUE);//Since we only went to Integer.MAX_VALUE -1
}
}
公共类测试{
公共静态void main(字符串参数[]){
长和=0;
int start_值=1;
int end_value=Integer.MAX_value-1;//以防万一
虽然(start_value除了其他答案外,由于永远无法达到最大整数值而陷入无限循环中,您还应该在循环中添加一个打印,以便查看进度:
public class Test {
public static void main(String args[]) {
long sum = 0;
int start_value = 1;
int end_value = Integer.MAX_VALUE - 1;//Just in case
while(start_value <= end_value){
sum += start_value;
start_value++;
//Print every 100 loops, change this if it prints too often
if (start_value % 100 == 0){
System.out.println("integer at: " + start_value + ", sum: " + sum);
}
}
System.out.println(sum + Integer.MAX_VALUE);//Since we only went to Integer.MAX_VALUE -1
}
}
公共类测试{
公共静态void main(字符串参数[]){
长和=0;
int start_值=1;
int end_value=Integer.MAX_value-1;//以防万一
尽管(start_value出于其他答案中指出的原因,对一个基元类型迭代以最大值或最小值为界的区间可能会非常棘手
Java8为此提供了一个新的解决方案,因为可以使用IntStream.rangeClosed
和LongStream.rangeClosed
在你的情况下,你可以这样做
IntStream.rangeClosed(1, Integer.MAX_VALUE).mapToLong(i -> i).sum();
或者只是
LongStream.rangeClosed(1, Integer.MAX_VALUE).sum();
出于其他答案中指出的原因,对于基元类型,在以最大值或最小值为界的区间上进行迭代可能非常棘手
Java8为此提供了一个新的解决方案,因为可以使用IntStream.rangeClosed
和LongStream.rangeClosed
在你的情况下,你可以这样做
IntStream.rangeClosed(1, Integer.MAX_VALUE).mapToLong(i -> i).sum();
或者只是
LongStream.rangeClosed(1, Integer.MAX_VALUE).sum();
您可以通过将int start\u value=1;
替换为long start\u value=1;
来解决此问题。非常感谢您得到了答案。您可以通过将int start\u value=1;
替换为long start\u value=1;
来解决此问题。非常感谢您得到了答案。谢谢……得到了答案谢谢……得到了答案