gpt4 book ai didi

JAVA无法将int转换为short

转载 作者:行者123 更新时间:2023-12-02 13:24:19 24 4
gpt4 key购买 nike

嗨,我是新来的,这是我的第一个问题:我有一个带有简单算术运算符的代码。但它不会起作用:

int a = 10;
short premennaTypuShort = (short)a; /*retyped variable a into short type and entered into new variable*/
premennaTypuShort = premennaTypuShort - 7;
/*now i am trying to decrease the number but eclipse wrote that cannot convert from int to short.*/

我正在尝试减少指定为short的数字,但eclipse写道无法从int转换为short。我不明白为什么。那么问题出在哪里呢?我该如何修复这个错误?

最佳答案

java 是 32 位的。这意味着每当执行任何算术运算时,它都会以 32 位值返回。因此,您必须再次将其转换为 Short,如下所示:

int a = 10;
short premennaTypuShort = (short)a;
premennaTypuShort =(short)(premennaTypuShort - 7);

关于JAVA无法将int转换为short,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37490382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com