Java回文直到EOF

时间:2016-10-06 00:38:16

标签: java eof palindrome

我有一个程序,应该询问用户一个数字,它将确定它是否是回文。在输入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");
}
}

2 个答案:

答案 0 :(得分:0)

这对我有用;除非你在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");
    }
}

答案 1 :(得分:0)

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

这样的单行方法来做到这一点
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");
}