我是编程的初学者。您能告诉我代码有什么问题吗?该代码未显示回文,尽管该数字是回文。
public static void main(String[] args) {
// TODO code application logic here
Scanner in = new Scanner(System.in);
int a =0;
int n =in.nextInt();
while(n >0){
int temp =n %10;
a = a*10+temp;
n = n/10;
}
System.out.println(a);
if( n ==a){
System.out.println("The number is palindrome");
}else{
System.out.println("The number is not palindrome");
}
}
输出:
16461
16461
The number is not palindrome
答案 0 :(得分:0)
您的代码可以正常工作,除了您没有保留原始值以将其与您计算出的反向数字进行比较之外。通过保存原始输入的副本,并在末尾使用它与您的计算结果进行比较,这可以为您的输入值起作用:
public static void main(String[] args) {
// TODO code application logic here
Scanner in = new Scanner(System.in);
int a = 0;
int n = in.nextInt();
int orign = n;
while(n >0){
int temp = n %10;
a = a*10+temp;
n = n/10;
}
System.out.println(a);
if( orign == a){
System.out.println("The number is palindrome");
}else{
System.out.println("The number is not palindrome");
}
}
示例会话:
16461
16461
The number is palindrome
12345
54321
The number is not palindrome
3
3
The number is palindrome