英文:
Can anyone tell me why it is giving error, while using long datatype
问题
long x = sc.nextLong();
if (x < 9223372036854775807L && x > -9223372036854775808L)
{
System.out.println("* long");
}
英文:
long x = sc.nextLong();
if (x < 9223372036854775807 && x > -9223372036854775808)
{
System.out.println("* long");
}
It's having error at if() condition line
>The literal 9223372036854775807 of type int is out of range
Can anyone tell me how to reslove it?
答案1
得分: 1
long x = sc.nextLong();
if (x < 9223372036854775807L && x > -9223372036854775808L) {
System.out.println("* long");
}
要指定一个数字字面量为 long 而不是 int,在字面量的末尾添加一个 "L"(表示 long)。如果末尾没有 "L",编译器会将这些数字识别为整数,并且在 if 语句中指定的整数超出了整数的范围。
<details>
<summary>英文:</summary>
long x = sc.nextLong();
if (x < 9223372036854775807L && x > -9223372036854775808L) {
System.out.println("* long");
}
To specify a numeric literal as a long instead of an int , add an L (for long) to the end of the literal. Without "L" in the end, the compiler is identifying the numbers as integers and the specified integers in the if statement are out of bounds of an Integer.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论