为什么线在while循环中重复?

时间:2015-02-26 22:28:12

标签: java arrays while-loop

我制作了一个简单的程序,可以选择添加,投票和查看3个不同候选人的投票。由于某种原因,第18行重复自身和它后面的第72行,例如,如果你开始,按1创建候选人,输入名称并按回车,它将通过这样:

Add candidate (1) or vote (2) or view number of votes on each candidate (3): Incorrect number, please try again.

然后又恢复正常......

Add candidate (1) or vote (2) or view number of votes on each candidate (3):

以下是整个代码(注明重复行):

package array;

import java.util.Scanner;

public class Array {

    public static void main(String[] args) {
        char[] Vote_Choice = {'A', 'B', 'C'};
        String[] candidates = { "none", "none", "none"};
        int[] votes = {0, 0, 0};


        Scanner input = new Scanner(System.in);
        boolean done = false;

        while (!done) {
            boolean done2 = false;
                System.out.print("Add candidate (1) or vote (2) or view number of votes on each candidate (3): ");   // here (line 18)

            switch (input.nextLine()) {
                case "1": System.out.print("Type new candidates name: ");
                    while (!done2) {
                        if (candidates[0].equals("none")) {
                        candidates[0] = input.next();
                        done2 = true;
                    }
                        else if (!candidates[0].equals("none") && candidates[1].equals("none")) {
                        candidates[1] = input.next();
                        done2 = true;

                    }
                    else if (!candidates[0].equals("none") && !candidates[1].equals("none") && candidates[2].equals("none")) {
                        candidates[2] = input.next();
                        done2 = true;
                    }
                    else if (!candidates[0].equals("none") && !candidates[1].equals("none") && !candidates[2].equals("none")) {
                        System.out.println("Sorry, all candidate slots are full.");
                        done2 = true;
                    }
                    }
                        break;
                case "2":
                    System.out.println("Type letter corresponding to the candidate you wish to vote for: ");

                    System.out.print("Type A for candidate "+ candidates[0] +", type B for candidate "+candidates[1]+", type C for candidate "+candidates[2]+": ");
                    String choice = input.next();
                    if (choice.equals("A")) {
                        votes[0]++;
                    }
                    if (choice.equals("B")) {
                        votes[1]++;
                    }
                    if (choice.equals("C")) {
                        votes[2]++;
                    }


                    break;
                case "3": if (!candidates[0].equals("none")) {
                    System.out.println("Candidate "+ candidates[0] +": "+votes[0]);
                } 
                            if (!candidates[1].equals("none")) {
                    System.out.println("Candidate "+ candidates[1] +": "+votes[1]);
                } 
                            if (!candidates[2].equals("none")) {
                    System.out.println("Candidate "+ candidates[2] +": "+votes[2]);
                } 
                            else {
                                System.out.println("Sorry, there are no current candidates.");
                            }
                break;
                default: System.out.println("Incorrect number, please try again.");  // And here, line 72
            }       

        }


    }
}

2 个答案:

答案 0 :(得分:1)

这是因为您有input.next()而不是input.nextLine()。线路终点"停留"在输入流中,当你调用它时,它返回该流中剩下的内容(通常是空字符串)。

答案 1 :(得分:0)

致电时:

candidates[1] = input.next();

这导致了问题,因为它只消耗部分输入。

如果您认为来自键盘的输入流可能类似于: 1 \ nDavid \ n

正在发生的事情是你正在阅读" 1 \ n"当你读到这一行时,那么当你读到" next"你读了#34; David",而不是新的行字符,所以下次调用nextLine时,你会得到空行,从\ n开始(\ n是一个新的行字符)。

您的代码应为:

import java.util.Scanner;

public class Array {

    public static void main(String[] args) {
        char[] Vote_Choice = { 'A', 'B', 'C' };
        String[] candidates = { "none", "none", "none" };
        int[] votes = { 0, 0, 0 };

        Scanner input = new Scanner(System.in);
        boolean done = false;

        while (!done) {
            boolean done2 = false;
            System.out
                    .print("Add candidate (1) or vote (2) or view number of votes on each candidate (3): "); // here
                                                                                                                // (line
                                                                                                                // 18)

            switch (input.nextLine()) {
            case "1":
                System.out.print("Type new candidates name: ");
                while (!done2) {
                    if (candidates[0].equals("none")) {
                        candidates[0] = input.nextLine();
                        done2 = true;
                    } else if (!candidates[0].equals("none")
                            && candidates[1].equals("none")) {
                        candidates[1] = input.nextLine();
                        done2 = true;

                    } else if (!candidates[0].equals("none")
                            && !candidates[1].equals("none")
                            && candidates[2].equals("none")) {
                        candidates[2] = input.nextLine();
                        done2 = true;
                    } else if (!candidates[0].equals("none")
                            && !candidates[1].equals("none")
                            && !candidates[2].equals("none")) {
                        System.out
                                .println("Sorry, all candidate slots are full.");
                        done2 = true;
                    }
                }
                break;
            case "2":
                System.out
                        .println("Type letter corresponding to the candidate you wish to vote for: ");

                System.out.print("Type A for candidate " + candidates[0]
                        + ", type B for candidate " + candidates[1]
                        + ", type C for candidate " + candidates[2] + ": ");
                String choice = input.nextLine();
                if (choice.equals("A")) {
                    votes[0]++;
                }
                if (choice.equals("B")) {
                    votes[1]++;
                }
                if (choice.equals("C")) {
                    votes[2]++;
                }

                break;
            case "3":
                if (!candidates[0].equals("none")) {
                    System.out.println("Candidate " + candidates[0] + ": "
                            + votes[0]);
                }
                if (!candidates[1].equals("none")) {
                    System.out.println("Candidate " + candidates[1] + ": "
                            + votes[1]);
                }
                if (!candidates[2].equals("none")) {
                    System.out.println("Candidate " + candidates[2] + ": "
                            + votes[2]);
                } else {
                    System.out
                            .println("Sorry, there are no current candidates.");
                }
                break;
            default:
                System.out.println("Incorrect number, please try again."); // And
                                                                            // here,
                                                                            // line
                                                                            // 72
            }

        }

    }
}