简体   繁体   English

回文计划

[英]Palindrome Program

I'm trying to write a program which will output what Palindromes will work from entering in a string and how many there are. 我正在尝试编写一个程序,该程序将输出从字符串输入返回的回文符以及有多少个。 I keep getting a lot of errors and I'm still trying to get my head around some of the harder topics in Java! 我不断收到很多错误,但我仍在努力解决Java中一些较难的主题!

Here's what I have already, as always, all answers are greatly appreciated! 与往常一样,这是我已经收到的所有答案,我们将不胜感激!

public static boolean Palindrome(String text) {
    int index;
    int palindrome;

    System.out.println("Please enter your text ");
    text = EasyIn.getString();
    for(index = 0; index < amount.length() / 2; index++) {
        if(text.charAt(index) != text.charAt(text.length() - index - 1)) {
            return false;
        }
    }
    System.out.println("The number of valid palindrome(s) is " + amount);
    amount = EasyIn.getString();
}

I think the problem is in amount.length() , you should use text.length() , since you are looping over the half of text . 我认为问题出在text.length() amount.length() ,您应该使用text.length() ,因为您要遍历一半的text The algorithm works fine. 该算法工作正常。 Here is a reduced example: 这是一个简化的示例:

public static boolean palindrome(String text)
{
    for (int index = 0; index < text.length() / 2; index++) {
        if (text.charAt(index) != text.charAt(text.length() - index - 1)) {
            return false;
        }
    }
    return true;
}

Note: 注意:

  • You forgot to add a return true statement, if you don't add one, is possible that the for loop finishes and no return statement is reached, which will cause an error. 您忘记添加一个return true语句,如果不添加,则可能是for循环完成并且没有到达return语句,这将导致错误。
  • I would recommend you to follow Java naming conventions. 我建议您遵循Java命名约定。 You method should be called like someMethodName instead of SomeMethodName . 您的方法应类似于someMethodName而不是SomeMethodName来调用。 This last is used for class names. 这最后一个用于类名。

Edit: As @bobbel commented, you could improve this code by assigning text.length() to a variable and using it inside the for . 编辑:正如@bobbel所说,您可以通过将text.length()分配给变量并在for使用它来改进此代码。

There can be two things: 可能有两件事:

ammount variable that you used either it could be a string array that you are maintaining strings inside it, if this is the case than you have to loop first through array of strings and then maintain one nested loop to check that strings inside it are palindrom or not 您使用的ammount变量可能是要在其中维护字符串的字符串数组,如果是这种情况,则必须先遍历字符串数组,然后维护一个嵌套循环以检查其中的字符串是palindrom还是不

or second case is that you have used the variable incorrect it may be text instead of ammount 或第二种情况是您使用了错误的变量,它可能是文本而不是ammount

public static void main(String[] args) { 公共静态void main(String [] args){

    Scanner in = new Scanner(System.in);
    System.out.println("enter string to check for palidrome");
    String orginal = in.next();

    int start = 0;
    int middle = orginal.length()/2;
    int end = orginal.length() - 1;
    int i;

    for(i=start; i<=middle; i++) {
        if(orginal.charAt(start) == orginal.charAt(end)) {
            start++;
            end--;
        } else {
            break;
        }
    }

    if(i == middle+1) {
        System.out.println("palidrome");
    } else {
        System.out.println("not palidrome");
    }
} 

This is the Simplest way of Checking Palindrom number . 这是检查Palindrom编号的最简单方法。

package testapi;

public class PalindromNumber {

    public static void checkPalindrom(Object number) {
        StringBuilder strNumber = new StringBuilder(number.toString());
        String reverseNumber = strNumber.reverse().toString();
        if (number.toString().equals(reverseNumber)) {
            System.out.println(number + " is palindrom number");
        } else {
            System.out.println(number + " is not palindrom number");
        }
    }

    public static void main(String[] args) {
        checkPalindrom(101);
        checkPalindrom(10.01);
        checkPalindrom("aanaa");
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM