[英]I am getting an unexpected integer value
I am debugging a program to which the output should look like this, (though the indents are a bit wonky): 我正在调试一个输出看起来像这样的程序(尽管缩进有点奇怪):
Enter a number you dislike 9
9
X9
_________
81
x12345679
_________
Surprise 999999999
X1000
_________
"No Surprise"? 999999999000
This is my code: 这是我的代码:
import java.util.Scanner;
import java.lang.Math;
public class Prog76a
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);
//Prompting the user to enter a number
System.out.print("Enter a number you dislike ");
int num = keyboard.nextInt();
int numA = num * 9;
int numB = numA * 12345679;
int numC = numB * 1000;
System.out.println(" ");
System.out.println(" " + num);
System.out.println(" X" + num);
System.out.println(" ____________");
System.out.println(" " + numA);
System.out.println(" x" + 12345679);
System.out.println(" ____________");
System.out.println("Surprise " + numB);
System.out.println(" X" + 1000);
System.out.println(" ____________");
System.out.println("\"No Surprise\"? " + numC);
}
}
And this is what it is coming up with: 这就是它的想法:
Enter a number you dislike 9
9
X9
____________
81
x12345679
____________
Surprise 999999999
X1000
____________
"No Surprise"? -727380968
I have absolutely no clue in this world how/why the final line came out to be "-727380968" Any help would be very appreciated. 在这个世界上,我绝对不知道如何/为什么最后一行是“ -727380968”。任何帮助将不胜感激。
This is probably overflowing int
, which has a max value of around 2 billion. 这可能是溢出的
int
,其最大值约为20亿。 Try using long
instead of int
. 尝试使用
long
代替int
。
Here is the javadoc showing exactly the max value of an int
: https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#MAX_VALUE 这是精确显示
int
最大值的javadoc: https : //docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#MAX_VALUE
Here is a quick link I found which shows why the underlying bit math turns a big number into something the computer reads as a negative number: https://www.cs.drexel.edu/~introcs/F2K/lectures/5_Scientific/overflow.html 这是我发现的一个快速链接,该链接显示了为什么底层位数学将大数字变成计算机读取为负数的内容: https : //www.cs.drexel.edu/~introcs/F2K/lectures/5_Scientific/overflow .html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.