Java for循环跳过运行两次

时间:2011-11-22 19:59:45

标签: java loops repeat

在让我有机会回答之前,我的代码似乎重复了两次for循环。我一直都在盯着这一天,而且我很确定我对我所做的任何错误视而不见。一些帮助将非常感激。

我很抱歉挪威人被困在那里,但这与问题无关。

public static void verb(){
    System.out.print("Oppgi navn på verbfil: ");
    Scanner input = new Scanner(System.in);
    String fileName = input.nextLine();
    File textFile = new File(fileName);
    try{
        Scanner fileInput = new Scanner(textFile);
        System.out.print("\nVelkommen til programmet som tester deg i bøyning av engelske verb.\nDu kan velge opp til 88 verb å bli testet i.\nDu vil få en vurdering når testen er ferdig\n\n");
        System.out.print("Ønsker du å prøve dette? (ja/nei) ");
        String yesNo = input.next();
        if (yesNo.toLowerCase().equals("ja")){
            System.out.print("\nOppgi ønsket antall verb: ");
            int points = 0;
            int amountVerbs = input.nextInt();
            for (int i = 1; i <= amountVerbs; i++){
                String verb1 = fileInput.next();
                String verb2 = fileInput.next();
                String verb3 = fileInput.next();
                int verbSelect = randomGen(3);
                if (verbSelect == 1){
                    System.out.print("... " + verb2 + " - " + verb3);
                    System.out.print("\nSkriv inn formen som mangler: ");
                    String answer = input.nextLine();
                    if (answer.toLowerCase().equals(verb1)){
                        points+= 1;
                    }
                }
                if(verbSelect == 2){
                    System.out.print(verb1 + " - " + "..." + " - " + verb3);
                    System.out.print("\nSkriv inn formen som mangler: ");
                    String answer = input.nextLine();
                    if (answer.toLowerCase().equals(verb2)){
                        points+= 1;
                    }
                }
                if(verbSelect == 3){
                    System.out.print(verb1 + " - " + verb2 + " - " + "...");
                    System.out.print("\nSkriv inn formen som mangler: ");
                    String answer = input.nextLine();
                    if (answer.toLowerCase().equals(verb3)){
                        points+= 1;
                    }
                }
            }
            System.out.print("Oppgi navnet ditt: ");
            String name = input.nextLine();
            System.out.print(name + "\n" + "Score: " + points + " av " + amountVerbs);
        } else{
            System.out.print("Greit det.");
        }
    } catch (FileNotFoundException exc) {
        System.out.println("Feil filnavn!");
    }
}

public static int randomGen(int randMax){
    Random generator = new Random();
    int verbSelect = generator.nextInt(randMax) + 1;
    return verbSelect;
}

}

1 个答案:

答案 0 :(得分:4)

您的问题可能来自您使用Scanner对象以及它如何处理End-Line(EOL)令牌。请注意,Scanner#nextLine()next()nextInt()等只有nextDouble()处理此令牌。通常,如果您让用户输入数字,则在获取数字输入后立即调用nextLine()是值得的。

如,

Scanner input = new Scanner(System.in);
int myInt = input.nextInt();
input.nextLine(); // added to swallow the EOL token

考虑改变这个:

String yesNo = input.next();

到这个

String yesNo = input.nextLine(); // handles EOL.

和此:

int amountVerbs = input.nextInt(); 

到此:

int amountVerbs = input.nextInt();
input.nextLine(); // to swallow the EOL token.

以及其他用于扫描仪的其他用途。