Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
回文单词没有正确显示 套餐学校; 导入java.util.*; 公共类回文词{ 布尔回文(字符串S){ 字符串检查=”; 对于(int i=S.length()-1;i>=0;i--){ 检查=检查+S.charAt(i); } 如果(检查等信号情况){ 返回true; } 否则{ 返回false; } } 公共静态void main(字符串参数[]){ 回文词ob=新回文词(); 扫描仪sc=新的扫描仪(System.in); System.out.println(“输入句子”); 字符串S=sc.nextLine(); S=S+“”; int标志=0,i=0; 字符串字; 对于(i=0;i_Java_String_Search_Palindrome - Fatal编程技术网

回文单词没有正确显示 套餐学校; 导入java.util.*; 公共类回文词{ 布尔回文(字符串S){ 字符串检查=”; 对于(int i=S.length()-1;i>=0;i--){ 检查=检查+S.charAt(i); } 如果(检查等信号情况){ 返回true; } 否则{ 返回false; } } 公共静态void main(字符串参数[]){ 回文词ob=新回文词(); 扫描仪sc=新的扫描仪(System.in); System.out.println(“输入句子”); 字符串S=sc.nextLine(); S=S+“”; int标志=0,i=0; 字符串字; 对于(i=0;i

回文单词没有正确显示 套餐学校; 导入java.util.*; 公共类回文词{ 布尔回文(字符串S){ 字符串检查=”; 对于(int i=S.length()-1;i>=0;i--){ 检查=检查+S.charAt(i); } 如果(检查等信号情况){ 返回true; } 否则{ 返回false; } } 公共静态void main(字符串参数[]){ 回文词ob=新回文词(); 扫描仪sc=新的扫描仪(System.in); System.out.println(“输入句子”); 字符串S=sc.nextLine(); S=S+“”; int标志=0,i=0; 字符串字; 对于(i=0;i,java,string,search,palindrome,Java,String,Search,Palindrome,您应该将增加标志的步骤移到if语句之外。否则,它仅在第一个单词是回文时有效 package school; import java.util.*; public class PalindromeWords { boolean palindrome(String S) { String check=""; for(int i = S.length()-1;i>=0;i--) { check = check+S.charAt

您应该将增加标志的步骤移到if语句之外。否则,它仅在第一个单词是回文时有效

package school;
import java.util.*;
public class PalindromeWords {
    boolean palindrome(String S) {
         String check="";
         for(int i = S.length()-1;i>=0;i--) {
             check = check+S.charAt(i);
         }
         if(check.equalsIgnoreCase(S)) {
             return true;
         }
         else {
             return false;
         }
    }
    public static void main(String args[]) {
        PalindromeWords ob = new PalindromeWords();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the sentence.");
        String S=sc.nextLine();
        S = S + ' ';
        int flag = 0,i=0;
        String word;
        for(i=0;i<S.length();i++) {
            if(S.charAt(i)==' ') {
                word = S.substring(flag,i);
                if(ob.palindrome(word)) {
                    System.out.println(word);
                    flag =i+1;
                }
            }
        }
    }
}

在循环外部使用标志

 if(ob.palindrome(word)) {
      System.out.println(word);     
 }
 flag = i+1;
这段代码应该可以用。

布尔回文(字符串S){
if(ob.palindrome(word)==true) {
    System.out.println(word);
}
flag =i+1;
字符串检查=”; 对于(int i=S.length()-1;i>=0;i--) { 检查=检查+S.charAt(i); } 如果(检查.equalsIgnoreCase)==真) { 返回true; } 其他的 { 返回false; } } 公共静态void main(字符串参数[]) { Para ob=新的Para(); 扫描仪sc=新的扫描仪(System.in); System.out.println(“输入句子”); 字符串S=sc.nextLine(); S=S+“”; int标志=0,i=0; 字符串字;
对于(i=0;i,这里有另一个解决方案,如下所示:

代码:

     boolean palindrome(String S){
     String check="";
     for(int i = S.length()-1;i>=0;i--)
     {
         check = check+S.charAt(i);
     }
     if(check.equalsIgnoreCase(S)==true)
     {
         return true;
     }
     else
     {
         return false;
     }
  }
public static void main(String args[])
{
    Para ob = new Para();
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the sentence.");
    String S=sc.nextLine();
    S = S + ' ';
    int flag = 0,i=0;
    String word;
    for(i=0;i<S.length();i++)
    {
        if(S.charAt(i)==' ')
        {
        word = S.substring(flag,i);
        if(ob.palindrome(word)==true)
        {
            System.out.println(word);

        }flag =i+1;
    }
  } 
}
import java.util.*;
public class PalindromeWords
{
    boolean isPalindrome(String S)
    {
        boolean result = false;
        String check="";
        for(int i = S.length()-1;i>=0;i--)
        {
            check = check+S.charAt(i);
        }
        if(check.equalsIgnoreCase(S))
        {
            result = true;
        }
        return result;
    }
    public static void main(String args[])
    {
        PalindromeWords ob = new PalindromeWords();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the sentence.");
        String str = sc.nextLine();
        String[] words = str.split(" ");
        for(int i=0; i < words.length; i++)
        {
            if(ob.isPalindrome(words[i]))
            {
                System.out.println(words[i]);
            }
        }
    }
}

您是否已使用调试器单步执行程序并查看正在检查的单词?为什么
if(check.equalsIgnoreCase==true)
而不是
if(check.equalsIgnoreCase==true)==true)
?@AnikMukherjee如果这解决了您的问题,请。
Enter the sentence.
hello mAdam
mAdam