给定一个字符串 A ,如果它是回文就打印 Yes ,否则打印 No

Given a A string , print Yes if it is a palindrome, print No otherwise

我想检查用户输入是否为回文。但这没有按预期工作。请帮忙。

我试过的代码:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        for (int i=0; i<A.length()/2; i++){
            if(A.charAt(i)==A.charAt(A.length()-i-1)) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }
}

您正在为字符串另一端具有相同字符的每个字符打印 "Yes",否则打印 "No"。将你的循环修改为 return 一个布尔值,它告诉 String 是否是回文,然后像这样在最后打印它:

boolean isPalindrome = true;
for (int i=0; i<A.length()/2; i++){
    if(!A.charAt(i)==A.charAt(A.length()-i-1)){
    isPalindrome = false;            
    }
}

if(isPalindome){
    System.out.println("Yes");
} else {
    System.out.println("No");
}
import java.util.Scanner;

public class test {

    public static void main(String args[]) {

        Scanner sc=new Scanner(System.in);
        String A=sc.nextLine();

        String temp="";
        for(int i=A.length()-1;i>=0;i--){
            temp = temp + A.charAt(i);
        }
        if(A.equals(temp)){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    }
}
    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    String A = sc.next();

    String reverse = new StringBuffer(A).reverse().toString();
    if (A.equals(reverse))
        System.out.println("Yes");
    else
        System.out.println("No");

}
 int end = A.length()-1;
    int i=0;
    boolean isPol = true;
    while (i<=end){
        if (A.charAt(i)!=A.charAt(end))
            isPol = false;
        i++;
        end--;
    }
    if (isPol == true) System.out.println("Yes");
    else System.out.println("No");
import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String A = sc.next();
        /* Enter your code here. Print output to STDOUT. */
        String reverse = "";
        int length = A.length();
        for (int i=length-1;i>=0;i--){
            reverse = reverse + A.charAt(i);
        }
        boolean ans = A.equals(reverse);
        if (ans) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}