字符串对称程序 [英] String Symmetry Program

查看:91
本文介绍了字符串对称程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我可以使用一点帮助来弄清楚为什么我的程序无法正常工作.问题是要创建一个使用递归的程序,该程序在删除所有标点和空格后给出的文字是否为回文.到目前为止,程序已编译,但返回的每个值都为false.我们只允许更改isSymmetrical方法.我可以使用任何可能的帮助来试图弄清楚如何进行这项工作.谢谢.

Hey I could use a little bit of help to figure out why my program isn't working. The question is to make a program using recursion that figures if it the text given is a palindrome or not after all the punctuation and white spaces are removed. While the program so far compiles, it returns every value as false. We are only allowed to change the isSymmetrical method. I could use whatever help possible trying to figure out how to make this work. Thank you.

public class StringSymmetry {

public static boolean isSymmetrical(String inputText)
{
    if(inputText.length() == 0 || inputText.length() ==1)
        return true;

    if(inputText.charAt(0) == inputText.charAt(inputText.length()-1))
        return isSymmetrical(inputText.substring(1,inputText.length()-1));

        return false;
}



public static void main(String[] args) {
    String[] sampleData = 
        { "Don't nod",
          "Dogma: I am God",
          "Too bad - I hid a boot",
          "Rats live on no evil star",
          "No trace; not one carton",
          "Was it Eliot's toilet I saw?",
          "Murder for a jar of red rum",
          "May a moody baby doom a yam?",
          "Go hang a salami; I'm a lasagna hog!",
          "Name is Bond, James Bond"
        };

    for (String s : sampleData)
    {
        System.out.println("isSymmetrical (" + s + ") returns " + isSymmetrical(s));
    }


}

}

推荐答案

问题是您没有包括大小写或标点符号以及空格的任何检查.

The problem is that you didn't include any checks for case or punctuation and white space.

您可以采用的一种方法是这样.具体信息取决于您允许用于分配的内容,但是您可能打算按照这些原则进行操作.

One way you could do it is something like this. The specifics depend on what you're allowed to use for the assignment, but you're probably intended to do something along these lines.

此外,请注意,如果您将默认语言环境设置为不寻常的内容(例如土耳其),则toLowerCase是有问题的.为了获得适当的鲁棒性,您需要指定语言环境,但这不是您在作业分配中要担心的事情.

Also, note that toLowerCase is problematic if you have the default locale set to something unusual like Turkey. For proper robustness, you need to specify a locale, but this isn't something you'll have to worry about in a homework assignment.

  public static boolean isSymmetrical(String inputText)
  {
      inputText = inputText.toLowerCase();

      if(inputText.length() == 0 || inputText.length() ==1)
          return true;

      if(!Character.isLetter(inputText.charAt(0)))
        return isSymmetrical(inputText.substring(1,inputText.length()));

      if(!Character.isLetter(inputText.charAt(inputText.length()-1)))
        return isSymmetrical(inputText.substring(0,inputText.length()-1));      

      if(inputText.charAt(0) == inputText.charAt(inputText.length()-1))
          return isSymmetrical(inputText.substring(1,inputText.length()-1));

      return false;
  }

这篇关于字符串对称程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆