问题描述
我试图做一个涉及泛数数字的Euler项目问题,该数字具有前5个质数的特殊除数要求,并认为这将是起点(请参阅1023456789是第一个要查看的数字,以及9876543210是最后一个).
I was trying to do some Project Euler question which involves pandigital numbers with special divisibility requirements of the first 5 prime numbers, and thought that this would be the starting point (see, 1023456789 is the first number being looked at, and 9876543210 is the last one).
import java.util.*;
public class pandigital_special
{
public static void main (String args[])
{
for (long l = 1023456789; l <= 9876543210; l++)
{
}
}
}
谁能告诉我为什么编译器声称for循环仅支持整数?我从未听说过.也就是说,编译器会说"int类型的字面值9876543210超出范围".
Can anyone tell me why the compiler claims that for loops only support integers? I have never heard of that. That is to say, the compiler says "The literal 9876543210 of type int is out of range".
推荐答案
您的代码可以正常工作.唯一的问题是您的第二个数字(9876543210)不在int范围内,但是您使用的是int文字.
Your code works fine. The only problem is that your second number (9876543210) is out of the int range, but you are using an int literal.
要使用长文字,只需在数字的末尾附加一个L:
To use a long literal, simply append an L to the end of the number:
9876543210L
这是一个更完整的示例:
Here is a more complete example:
public class Test {
public static void main(String[] args){
for (long l = 1023456789L; l <= 9876543210L; l++){
System.out.println(l);
}
}
}
这篇关于长时间不支持Java for循环类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!