我试图找到0 &给定的输入。代码中有一些错误。我觉得问题出在逻辑上。
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
int count = 0;
System.out.println("Enter the limit to check the no of Palindrome ");
Scanner input = new Scanner(System.in);
int no = input.nextInt();
for (int j = 0; j <= no; j++) {
if (number(j)) ;
++count;
}
System.out.println(count);
}
public static boolean number(int num) {
int i = num;
int reverse = 0;
while (i != 0) {
reverse = reverse * 10;
reverse = reverse + i % 10;
i = i / 10;
}
if (num == reverse) {
return true;
} else
return false;
}
}
if
语句以分号结尾视为空语句。因此,如果您从if条件的末尾删除;
,您应该很好地获得0到n之间的回文计数。还要注意Java中的整数最大范围为2,147,483,647,这意味着如果您将其输入为n
值,它将溢出,因此您应该选择j
类型。
if(number(j));
^