What Im trying to code is that a user inputs a number n and the program outputs all the numbers from 1 to n that are both prime numbers and palindrome numbers. I created a method for finding prime numbers and another for finding if a number is a palindrome. My code for outputting the integers that are both goes like
我试图编码的是用户输入数字n,程序输出从1到n的所有数字,它们都是素数和回文数。我创建了一种查找素数的方法,另一种用于查找数字是否为回文数据。我输出整数的代码都是这样的
if ((prime(y)==true) && (pal(y)==n)) {
System.out.println(y);
}
Here is my method for finding prime numbers:
这是我查找素数的方法:
public static boolean prime(int n) {
int x = 2;
while (n%x>0) {
x+=1;
} if (x==n) {
return true;
} else {
return false;
}
}
Here's my method for finding palindrome numbers:
这是我查找回文数的方法:
public static boolean pal(int n) {
int rev = 0;
int rmd = 0;
while (n>0) {
rmd = n%10;
rev = rev*10 + rmd;
n = n/10;
} if (rev==n) {
return true;
} else {
return false;
}
}
I get the error that these two methods are not comparable because apparently one is boolean and the other integer. Anyone know how to fix this?
我得到这两个方法无法比较的错误,因为显然一个是布尔值而另一个是整数。有人知道怎么修这个东西吗?
1 个解决方案
#1
3
Your statement says pal(y)==n
, but pal(y)
returns a boolean. I'm not sure what comparing it to n
is supposed to do.
你的陈述说pal(y)== n,但是pal(y)返回一个布尔值。我不确定将它与n进行比较应该做什么。
In general, avoid statements like foo == true
. If it's already a boolean, just use foo
, or for false !foo
.
通常,避免使用foo == true之类的语句。如果它已经是布尔值,只需使用foo,或者使用false!foo。
#1
3
Your statement says pal(y)==n
, but pal(y)
returns a boolean. I'm not sure what comparing it to n
is supposed to do.
你的陈述说pal(y)== n,但是pal(y)返回一个布尔值。我不确定将它与n进行比较应该做什么。
In general, avoid statements like foo == true
. If it's already a boolean, just use foo
, or for false !foo
.
通常,避免使用foo == true之类的语句。如果它已经是布尔值,只需使用foo,或者使用false!foo。