Java 回文直到 EOF

Java palindrome until EOF

我有一个程序应该向用户询问一个数字,它会确定它是否是回文。它应该一直询问数字,直到输入 EOF - 到目前为止,它两次询问数字并且似乎没有正确执行 while 循环。

如有任何见解,我们将不胜感激

import java.util.Scanner;
public class PalindromeEOF
{
public static void main(String args[])
{
    Scanner scanner = new Scanner(System.in);        
    System.out.println("Enter a number to check if it is a palindrome:");

    String num = scanner.nextLine();
    String reverse = "";

    while (scanner.hasNextLine())
    {
        for ( int i = 0; i<num.length(); i++ )
        {
            reverse = num.charAt(i) + reverse;
        }
        if (num.equals(reverse))
        {
            System.out.println("\nEntered number IS a palindrome.");
        }
        else
        {
            System.out.println("\nEntered number is NOT a palindrome.");
        }
        System.out.println("\nEnter a number to check if it is a palindrome:");
        num = scanner.nextLine();
        reverse = "";
    }
    System.out.println("\nProgram ended on request");
}
}

这对我有用;除非你在 while 循环之外需要 numreverse,否则它应该可以工作。

import java.util.Scanner;
public class PalindromeEOF
{
    public static void main(String args[])
    {
        Scanner scanner = new Scanner(System.in);        
        System.out.println("Enter a number to check if it is a palindrome:");

        while (scanner.hasNextLine())
        {
            String num = scanner.nextLine();
            String reverse = "";

            for ( int i = 0; i<num.length(); i++ )
            {
                reverse = num.charAt(i) + reverse;
            }
            if (num.equals(reverse))
            {
                System.out.println("\nEntered number IS a palindrome.");
            }
            else
            {
                System.out.println("\nEntered number is NOT a palindrome.");
            }
            System.out.println("\nEnter a number to check if it is a palindrome:");

        }
        System.out.println("\nProgram ended on request");
    }
}

我会把回文测试分成它自己的方法。你可以用像

这样的一行方法来做到这一点
public static boolean isPalindrome(String str) {
    return new StringBuilder(str).reverse().toString().equals(str);
}

但我更愿意迭代字符的前半部分并将它们与后半部分反向比较,如

public static boolean isPalindrome(String str) {
    if (str == null) {
        return false;
    }
    char[] chars = str.toCharArray();
    for (int i = 0; i * 2 <= chars.length; i++) {
        if (chars[i] != chars[chars.length - i - 1]) {
            return false;
        }
    }
    return true;
}

然后您的 main 可以在无限循环中调用它(在缺少输入时终止),例如

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while (true) {
        System.out.println("Enter a number to check if it is a palindrome:");
        if (!scanner.hasNextLine()) {
            break;
        }
        String num = scanner.nextLine();
        if (isPalindrome(num)) {
            System.out.printf("%s is a palindrome%n", num);
        } else {
            System.out.printf("%s is NOT a palindrome%n", num);
        }
    }
    System.out.println("Program ended on request");
}