Palindrome词语没有正确显示

问题描述 投票:2回答:4
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;
                }
            }
        }
    }
}

我被赋予了一项任务,我必须编写一个Java程序来打印一个句子中的所有Palindrome单词。这是我写的代码,我没有得到正确的输出。

enter image description here

如您所见,控制台中的输出不会在结果中提供查询。

java string search palindrome
4个回答
5
投票

你应该移动增加旗帜在if statement之外的步骤。否则,它仅在第一个单词是回文时才有效。

 if(ob.palindrome(word)) {
      System.out.println(word);     
 }
 flag = i+1;

1
投票

使用循环外的标志。

if(ob.palindrome(word)==true) {
    System.out.println(word);
}
flag =i+1;

这段代码应该有效。


0
投票

这是另一种解决方案如下:

码:

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]);
            }
        }
    }
}

输出:

Enter the sentence.
hello mAdam
mAdam

-2
投票
     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;
    }
  } 
}
© www.soinside.com 2019 - 2024. All rights reserved.