我的循环或扫描仪有问题

时间:2014-04-19 07:42:25

标签: java loops for-loop input java.util.scanner

我正在尝试制作一个程序来检查短语是否是回文。我首先询问他们要检查多少个短语,然后继续要求用户输入短语。

出于某种原因,当我在测试时将短语数量设为1时,程序会跳过询问用户输入的用户,并直接打印出结果..

我猜测问题在于用于询问用户输入的For循环,但我不确定..

我会粘贴程序的主要方法,因为我认为现在还不需要其他方法..

public static void main(String [] args)
{
    //vars
    String userInput;
    //scanner
    Scanner in = new Scanner(System.in);
    //user input
    System.out.println("\t Palindrome Checker");
    System.out.print("How many do you want to test: ");
    int many = in.nextInt();
    //Array
    String [] a = new String[many];
    boolean [] det = new boolean[many];
    for(int i = 0; i < many; i++)
    {
        System.out.print("Input phrase one at a time: ");
        a[i] = in.nextLine();
    }

    //object
    RecursivePalindrome pal = new RecursivePalindrome();

    System.out.println();


    //call methods
    for(int i = 0; i < many; i++)
    {
        a[i] = RecursivePalindrome.helper(a[i]);
        det[i] = pal.palindrome(a[i]);
    }

    //print results
    System.out.println("=========================== Results ============================");
    System.out.println();
    System.out.printf(" %-30s%-11s%n", "Phrase or Word", "Palindrome?");


    for(int i = 0; i < a.length; i++)
    {

        System.out.printf(" %-30s%-11s%n", a[i], det[i]);
    }
}

0 个答案:

没有答案