问题
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++)
{
}
}
}
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".
回答1:
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.
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);
}
}
}
回答2:
in your for loop
for (long l = 1023456789; l <= 9876543210; l++)
you have number 9876543210
which is too large for int change it to
9876543210L
updated code will look like this
for (long l = 1023456789; l <= 9876543210L; l++)
回答3:
I think you have to have an L after to declare it as a long, like this:
long l = 1023456789L
来源:https://stackoverflow.com/questions/25999966/java-for-loop-type-long-not-supported