英文:
Moving the contents of a float to a long in Java
问题
以下是翻译好的内容:
这个问题在网站上的示例是:
“将一个声明为浮点数据类型的变量的内容移动到一个声明为长整型数据类型的变量中,需要使用_____。”
答案:一个类型转换
解释:需要显式类型转换“float”
long wayHome = 123456789;
float myBoat = (float) wayHome;
但是在我看来,这似乎是在相反的方向上进行操作 —— 将一个长整型变量转移到一个浮点型变量中,而不是相反的操作。
我是错的,还是问题本身有问题?是隐式转换还是类型转换?
英文:
The example given for this problem on the site is:
"To move the contents of a variable declared as data type float into a variable declared as data type long requires the use of _____."
Answer: a type cast
Explanation: The explicit type cast "float" is required
long wayHome = 123456789;
float myBoat = (float) wayHome;
But this appears to be doing the opposite to me -- moving a long into a float, not the other way around.
Am I wrong, or is the question wrong? Would it be implicit or typecast?
答案1
得分: 3
给定的示例确实与问题文本中描述的相反,就像你所假设的那样。
然而,将一个 float
值分配给一个 long
变量也需要显式转换,例如:
float myBoat = 123.456;
long wayHome = (long) myBoat;
英文:
The example given is indeed the opposite of what's described in the question's text, like you assumed.
However, assigning a float
value to a long
variable will also require an explicit cast, e.g.:
float myBoat = 123.456
long wayHome = (long) myBoat;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论