简体   繁体   English

我得到了意外的整数值

[英]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.

相关问题 我在Java 8代码列表中将Integer值获取为'Integer' - I am getting Integer value as 'Integer' in the list in my Java 8 code 为什么我在使用最大 integer 值时得到荒谬的值? - Why I am getting absurd values on using maximum integer value? 为什么我收到错误作为意外令牌 - why I am getting error as unexpected token 为什么我收到错误:需要意外类型:找到变量:值 - Why am I getting the Error: unexpected type required: variable found: value 我得到了SimpleDateFormat.parse()的意外输出。 - I am getting an unexpected output of SimpleDateFormat.parse(). 使用Java Streams和Scanners时,我遇到了意想不到的行为 - I am getting unexpected behaviour when using Java Streams and Scanners 我正在尝试查询并得到意外的令牌:异常 - I am trying following query and getting unexpected token : in exception 为什么我在ArrayList中而不是整数值中得到此结果? - Why am I getting this results in my ArrayList instead of the integer values? 为什么在将字符串转换为整数时会出现NumberFormatException? - Why am I getting NumberFormatException while converting a string to integer? 为什么在将数字转换为整数的前面得到“ 0”? - Why I am getting “0” in front part of transforming digits into integer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM