简体   繁体   中英

Java for loop skips runs twice

My code seems to repeat the for loop twice, before giving me the opportunity to answer. I've been staring at this all day, and I'm pretty sure I'm blind to whatever error I've made. Some help would be much appreciated.

I'm sorry for the Norwegian stuck in there, but it's not relevant to the problem.

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;
}

}

Your problem is likely from your use of the Scanner object and how it handles the End Of Line (EOL) token. Note that only Scanner#nextLine() handles this token while next() , nextInt() , nextDouble() and the like do not. Often if you have the users enter a number, it is worthwhile calling nextLine() immediately after getting the numeric input.

eg,

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

Consider changing this:

String yesNo = input.next();

to this

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

and this:

int amountVerbs = input.nextInt(); 

to this:

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

and so-forth for your other uses of Scanner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM