【问题标题】:Issue with Palindrome class using Stacks使用堆栈的回文类问题
【发布时间】:2015-02-12 16:41:35
【问题描述】:

我正在尝试使用 Stacks 编写回文类来确定用户输入的单词是否是回文。我的回文课似乎有问题。有人可以帮我识别吗?我的程序有效,但无论我输入什么单词,它都会返回该单词不是回文。

import java.util.Stack;
public class Palindrome
{
     public Palindrome()
     {
        Stack stack = new Stack();
        String input = "";
        boolean isPalindrome = false;
        for(int i = 0; i < input.length(); i++)
        {
            stack.push(i);
        }

        String opposite = " ";
        while(!stack.isEmpty())
        {
            opposite = opposite + stack.pop();
        }

        if(input.equals(opposite))
            isPalindrome = true;
        else
            isPalindrome  = false;
    }//end main   
}//end class Palindrome 

这是我的 PalindromeCheck 课程:

import java.util.Scanner;
public class PalinDromeCheck
{
   public static void main(String[] args)
   {
       Palindrome pal = new Palindrome();
       Scanner type = new Scanner(System.in);       //create scanner for  user to type a word

       String word = null;                          //initial word to check
       String stop = null;                          //stops processing of word

       do
       {
           System.out.println("Enter a word to determine if it is a palindrome: ");     
           word = type.nextLine();                  //user types word

           if(pal.equals(word))                 
               System.out.println("is a palindrome");
           else
               System.out.println("is not a palindrome\n");

           System.out.println("Would you like to try another word? Type Y for 'Yes' or N for 'No'");
           stop = type.nextLine();                  //stops processing
       }
       while(stop.equalsIgnoreCase("y"));           //continues to process and ignores upper or lowercase Y
   }
}

【问题讨论】:

  • pal.equals(word) 比较两个给定对象。没有办法,你会得到正确的答案。请学习尝试学习使用class、constructor、methods 的基础知识,然后实现逻辑。你的回文检查码放错地方了。
  • 首先你检查回文的逻辑不应该在构造函数中,而是你可以创建一个静态方法来检查回文。就像@Prera​​kSola 所说的相等比较两个对象,并且由于您没有覆盖 equals 方法,它正在调用 Object 类的 equals 来检查实例是否是相同的引用。

标签: java stack palindrome


【解决方案1】:

你有几个错误。

  • 首先,您需要将输入提供给 Palindrome 类。
  • 其次,当您使用堆栈反转输入时,您将索引压入堆栈,而不是字符。
  • 第三,在构造函数中做所有事情不是一个好习惯。 Palindrome 类不需要知道作为成员的输入或用于初始化目的

如果你不需要有几个回文的实现,你应该使用静态方法。

试试这个:

public class Palindrome
{
    public static boolean isPalindrome(String input)
     {

         char[] inputArray = input.toCharArray();
         bool isOk = true;
         for(int i = 0; i < inputArray.length/2 && isOk; i++){
             isOk &= inputArray[i] == inputArray[inputArray.length - i - 1];
         }
         return isOk;
     } // end method
} //end class Palindrome 

那么,你的主要功能可能是:

public static void main(String[] args)
{
    System.out.println("Enter a word to determine if it is a palindrome: ");     
    word = type.nextLine();                  //user types word

    if(Palindrome.isPalindrome(word)) {        
        System.out.println("is a palindrome");
    } else {
        System.out.println("is not a palindrome\n");
    }
} // End of main

【讨论】:

    【解决方案2】:

    这是你的完整回文班吗?如果是,它没有输入要处理!

    public class Palindrome
    {
        public static boolean isPalindrome(String input)
         {
            Stack stack = new Stack();
    
            for(int i = 0; i < input.length(); i++)
            {
                stack.push(input.charAt(i));
            }
    
            String opposite = "";
            while(!stack.isEmpty())
            {
                opposite = opposite + stack.pop();
            }
    
            return input.equals(opposite);
    
        }//end main
    }//end class Palindrome 
    

    这会创建一个您可以在代码中使用的静态方法,例如:

    System.out.println("Enter a word to determine if it is a palindrome: ");     
    word = type.nextLine();                  //user types word
    
    if(Palindrome.isPalindrome(word))                 
        System.out.println("is a palindrome");
    else
        System.out.println("is not a palindrome\n");
    

    【讨论】:

    • isPalindrome 没有在你的方法中定义,你可以return input.equals(opposite);
    • 谢谢,我不小心删除了布尔值 o_O,至少有帮助吗?
    【解决方案3】:

    您的输入为空,因为您在构造函数中将输入设置为“”。您最好使用构造函数参数来包含输入,然后使用成员变量来跟踪它。

    您键入的代码可能更适合在方法中(可能等于?您必须覆盖它)。您可能需要考虑将输入字符串中的字符推入堆栈,而不是 0、1、2、3... n(其中 n = input.length() - 1)。

    您当前的.equals(word) 没有按照您的想法执行,因为您没有提供过载。

    【讨论】:

      猜你喜欢
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 2021-04-26
      • 2011-01-23
      • 2021-04-13
      相关资源
      最近更新 更多