伙计们请帮我纠正“111”和“222”因为我纠正了“333”及以上系列
Guys Plz help me to get correct out for "111" and "222" Because I got correct for "333" and above series
伙计们,我对回文有一个疑问。我为数字回文写了一个程序。它执行成功,但是当我输入 333 时我得到了正确的输出,当我输入 111 时我得到了错误的输出。我不知道出了什么问题,因为当我执行上面的 3 时会给出正确的输出,但是当我执行下面的 3 时我得到了错误的输出。请帮我解决我做错了什么。
public class Practice1 {
public static void main(String[] args) {
int num = 121, res = 0, var = 0, c = num;
for (int i = 1; i <= num; i++) {
res = num % 10;
var = var * 10 + res;
num = (num) / 10;
}
if (var == c) {
System.out.println(c + " is a Palindrome Number");
} else {
System.out.println(c + " is not a Palindrome Number");
}
}
}
你可以使用while
循环
public static void main(String[] args) {
int num = 121, res = 0, var = 0, c = num;
while (num != 0) {
res = num % 10;
var = var * 10 + res;
num = (num) / 10;
}
if (var == c) {
System.out.println(c + " is a Palindrome Number");
} else {
System.out.println(c + " is not a Palindrome Number");
}
}
伙计们,我对回文有一个疑问。我为数字回文写了一个程序。它执行成功,但是当我输入 333 时我得到了正确的输出,当我输入 111 时我得到了错误的输出。我不知道出了什么问题,因为当我执行上面的 3 时会给出正确的输出,但是当我执行下面的 3 时我得到了错误的输出。请帮我解决我做错了什么。
public class Practice1 {
public static void main(String[] args) {
int num = 121, res = 0, var = 0, c = num;
for (int i = 1; i <= num; i++) {
res = num % 10;
var = var * 10 + res;
num = (num) / 10;
}
if (var == c) {
System.out.println(c + " is a Palindrome Number");
} else {
System.out.println(c + " is not a Palindrome Number");
}
}
}
你可以使用while
循环
public static void main(String[] args) {
int num = 121, res = 0, var = 0, c = num;
while (num != 0) {
res = num % 10;
var = var * 10 + res;
num = (num) / 10;
}
if (var == c) {
System.out.println(c + " is a Palindrome Number");
} else {
System.out.println(c + " is not a Palindrome Number");
}
}