Java Palindrome程序(我正在按计划进行)吗? [英] Java Palindrome Program (am I on track)?

查看:62
本文介绍了Java Palindrome程序(我正在按计划进行)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只有6个月的Java经验(而且我在这里也是新手),所以如果代码中的内容看起来不正确,请多多包涵.请注意,它仍在进行中.我正在尝试编写一个程序,该程序可以接收字符串并仅打印属于回文的字符串.

I have only 6 months of Java experience (and I'm also new here) so please bear with me if things don't look entirely right in my code. Please note that it's still a work in progress. I'm trying to write a program that takes in strings and prints only the ones that are palindromes.

我应该: -创建名为 isPalindrome 的方法,该方法具有String参数,并且 -根据字符串是否为回文符返回布尔值.然后 -修改主要方法以使用 isPalindrome 仅打印回文.

I'm supposed to: - create a method named isPalindrome, which has a String parameter and - returns a Boolean based on whether the string is a palindrome or not. Then - modify the main method to use isPalindrome to print only the palindromes.

例如,如果输入:女士詹姆斯·苹果妈妈计时器",则应打印女士"和妈妈".

For example, if I type: "madam James apple mom timer", it should print "madam" and "mom".

这基本上是我要编写的程序: 例如:让我们使用女士"一词.程序将检查首字母和尾字母是否匹配(" m ada m ").如果是这样,它将检查下一个字母,这次是"a"和"a"("m a d a m"),依此类推,等等.

This is basically the program I am trying to write: Ex: Let's use the word "madam". The program will check if the first and last letters match ("madam"). If that is true, then it'll check the next letters, this time "a" and "a" ("madam). And so on and so forth.

这是我到目前为止拥有的Java代码:

This is the Java code I have so far:

public class Palindrome 
{
    private String theWord; //Error: The value of the field Palindrome.theWord is not used

    public boolean isPalindrome( String theWord ) {
        int firstPointer = 0;
        int secondPointer = theWord.length() - 1;

        for ( int i = 0; i < theWord.length( ); i++ ) {
            if ( theWord.charAt[0] == theWord.charAt (theWord.length() - 1) ) { //Error: charAt cannot be resolved or is not a field
                return true;
            }
            return false;
        }
    }


    public static void main( String[] theWord ) {
        Palindrome = new Palindrome( ); //Error: Palindrome cannot be resolved to a variable

        for ( int i = 0; i < theWord.length; i++ ) {
            while (firstPointer < secondPointer) { //Error: "firstPointer" cannot be resolved to a variable. "secondPointer" cannot be resolved to a variable
                if ( theWord.charAt[0] == theWord.charAt (theWord.length() - 1) ) {  //Error: charAt cannot be resolved to a variable or is not a field. Cannot invoke length() on the array type String[]
                    firstPointer++; //Error: "firstPointer" cannot be resolved to a variable
                    secondPointer++; //Error: "secondPointer" cannot be resolved to a variable
                }
                System.out.println(theWord);
            }
        }
    }
}

任何帮助我知道我错了的地方将不胜感激.请不要只给我正确的代码.我想弄清楚这一点.非常感谢.

Any bit of help knowing where I've gone wrong would be greatly appreciated. Please don't just give me the right code. I would like to figure this out. Thank you very much.

**我现在将错误作为注释包含在代码中.顺便说一下,我正在使用Eclipse.

** I've included the errors as comments in the code now. I'm using Eclipse by the way.

-> **好的.到目前为止,我已经阅读了您的大多数答案,并且能够更正我的大部分代码(到目前为止,非常感谢大家).我现在唯一仍然有问题的部分是此部分:

-->**EDIT 2: Okay guys. I've read most of your answers and have been able to correct most of my code so far (Thank you all so much so far). The only part I'm still having an issue with right now is this part:

if ( theWord.charAt(i) == theWord.charAt (theWord.length() - i - 1) ) {
                    leftPointer++;
                    rightPointer--;

我现在得到一个无法在数组类型String []上调用charAt(int)" 无法在数组类型String []上调用length()" . 那是剩下的仅有的两个错误,然后我将测试代码.我已经尝试解决它们一段时间了,但我仍然不确定这些错误的含义.

I'm now getting a "Cannot invoke charAt(int) on the array type String[]" and "Cannot invoke length() on the array type String[]". Those are the only two errors remaining, then I'll test the code out. I've been trying to resolve them for a while now but I'm still not entirely sure what those errors mean.

Eclipse建议我将 theWord.charAt(i)更改为 theWord.length ,这不是我想要的.这也建议我从 length 中删除()",但我也不认为这是正确的.

Eclipse is suggesting that I change theWord.charAt(i) to theWord.length which is not what I want. It is also suggesting I remove "( )" from length but I don't think that's right either.

推荐答案

查看您的isPalindrome方法:

if ( theWord.charAt(0) == theWord.charAt (theWord.length() - 1) 

在这里,您始终将第一个字符与最后一个字符进行比较.在每次迭代中,您应该比较不同的字符对,直到找到不匹配的对或到达单词中间.

here you always compare the first character to the last character. In each iteration you should compare a different pair of characters, until you find a pair that doesn't match, or reach the middle of the word.

您应该使用循环的i变量:

You should use the i variable of your loop :

if ( theWord.charAt(i) == theWord.charAt (theWord.length() - i - 1) 

并且返回值应该正好相反.如果找到一对不匹配的字符,则返回false.仅当循环结束而没有返回false时,才返回true.

And the return value should be the exact opposite. If you find a pair of characters that don't match, you return false. Only if the loop ends without returning false, you return true.

这篇关于Java Palindrome程序(我正在按计划进行)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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