Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java溢出在计算器上计算的结果与IDE结果不同吗?_Java - Fatal编程技术网

Java溢出在计算器上计算的结果与IDE结果不同吗?

Java溢出在计算器上计算的结果与IDE结果不同吗?,java,Java,我不明白我的计算哪里出了问题,为什么我在IDE控制台上收到了不同的结果。控制台接收myShortTotal的println的14464结果。但是通过在计算器上手动计算,我得到了一个类似的数字,但是+1,它是14465 package com.company; public class Main { public static void main(String[] args) { byte ByteValue = 10; short ShortValue = 20;

我不明白我的计算哪里出了问题,为什么我在IDE控制台上收到了不同的结果。控制台接收my
ShortTotal的
println
14464
结果。但是通过在计算器上手动计算,我得到了一个类似的数字,但是
+1
,它是
14465

package com.company;

public class Main {

    public static void main(String[] args) {
   byte ByteValue = 10;
   short ShortValue = 20;
   int IntValue = 50;
   long LongTotal = 50000 + 10 * (ByteValue + ShortValue + IntValue);
        System.out.println(LongTotal);
//The issue in question is below.
        short ShortTotal = (short) (1000 * (ByteValue + ShortValue + IntValue));
      System.out.println(ShortTotal);
    }
}
我了解短类型的最大值和最小值是什么。以下是我迄今为止所做的工作:

因为短最大值约为
32767
。因此,剩余的金额又降到了最低。因此,在计算器上,您可以通过以下操作进行检查:
1000 x(字节值+shortvalue+intvalue)=80000
。由于
80000
大于short max val,它会溢出剩余值
80000-32767=47233
然后将该值加回到最小值上,即:
-32768+47233=14465

我设法接近
14464
的值,但不知道为什么我会收到
14465

package com.company;

public class Main {

    public static void main(String[] args) {
   byte ByteValue = 10;
   short ShortValue = 20;
   int IntValue = 50;
   long LongTotal = 50000 + 10 * (ByteValue + ShortValue + IntValue);
        System.out.println(LongTotal);
//The issue in question is below.
        short ShortTotal = (short) (1000 * (ByteValue + ShortValue + IntValue));
      System.out.println(ShortTotal);
    }
}

我不理解你的困惑

80000作为
int
值给出以下位模式:

00000000000000010011100010000000
切掉前两个字节会为
short
值提供以下位模式:

0011100010000000
转换为十进制,这是:

14464
或者换一种说法:短字符可以表示2^16个可能的值:

80000 - (2^16) = 80000 - 65536 = 14464

在手动计算中,您将32767和-32768视为相同的数字,但它们相差1。代码是正确的,您的手动数学不是。无关:请遵循java命名约定。变量名使用camelCase,只有类使用大写camelCase。
(short)32767+1=(short)-32768
您忘记了+1只是一个小小的注释:请使用正确的变量名,这是其他人和社区更好地理解您的代码的方式。例如,“TotalLong”应该是“total”。关于更多细节,请阅读以下内容:感谢您的回复,我不清楚我的回复,因此我为您的回复道歉。这里的混乱不在代码中,而是我在确认14464结果后所做的手动计算。但通过人工计算,我得到了14465英镑。一个用户呼叫f1sh发表评论说,我将32767和-32768视为同一个数字,这可能是我正在努力解决的问题。您的最小值和最大值是正确的,但将零作为需要考虑的可能值保留在帐户之外。