简体   繁体   中英

Program appears to skip first iteration of while loop

What is causing my program to skip the first iteration of my while loop here?:

    import java.util.Scanner;

public class myfun {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int totalNumbers;
        int product = 0;
        int count = 0;
        int sum = 0;
        String choice = "choice";
        System.out.print("You can add up to ten numbers.\n");
        while(! choice.equalsIgnoreCase("stop")){
            System.out.print("Would you like to add or multiply? (Enter stop to be done)");
            choice = scan.nextLine();
            if (choice.equalsIgnoreCase("multiply")) {
                product = 0;
                System.out.print("How many numbers would you like to multiply?");
                totalNumbers = scan.nextInt();
                count = 0;
                System.out.print("Please enter " + totalNumbers +" numbers.");
                product +=scan.nextInt();
                count++;
                while (count < totalNumbers) {
                    product *= scan.nextInt();
                    count++;
                }
                System.out.println("The product is " + product);
            }
            else if (choice.equalsIgnoreCase("add")) {
                sum = 0;
                System.out.print("How many numbers would you like to add?");
                totalNumbers = scan.nextInt();
                count = 0;
                System.out.print("Please enter " + totalNumbers +" numbers.");
                sum +=scan.nextInt();
                count++;
                    while (count < totalNumbers) {
                        sum += scan.nextInt();
                        count++;
                    }
                System.out.println("The sum is " + sum);
            }
        }
        System.out.print("Come again!");
        scan.close();
    }
}

Replace you call to scan.nextLine() with scan.next() . The issue with Scanner.nextLine() is that it captures the newline character added after the last integer (2 in you example).

In your case, you just want one word input(add/multiple/stop), so you can just use Scanner.next() function and it will work fine.

For more info, you can go through this SO thread.

This set of instructions are behaving as expected. scan.nextLine() reads value till it finds line separator. since you are scanning line before any input, execution controller will not move further till it finds line separator.

Root Cause : nextLine() method takes the empty string ie "" as input in your iteration.

Solution : You can use the next() method to solve this issue.

Modify the following line

choice = scan.nextLine();

to

choice = scan.next();

I think your structure of code is not perfect. We don't want to put count variable because using for loop you can reduce your code. We initialize product = 1 because 0 * 5 = 0 .

Here down is modified code:

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int totalNumbers;
        int product;
        int sum;
        int i;
        String choice = "";
        System.out.print("You can add up to ten numbers.\n");
        System.out.print("Would you like to add or multiply? (Enter stop to be terminate)");
        choice = scan.next();
        if (choice.equalsIgnoreCase("add") || choice.equalsIgnoreCase("multiply") || choice.equalsIgnoreCase("stop")) {
            while (!choice.equalsIgnoreCase("stop")) {
                if (choice.equalsIgnoreCase("multiply")) {
                    product = 1;
                    System.out.print("How many numbers would you like to multiply?");
                    totalNumbers = scan.nextInt();
                    System.out.print("Please enter " + totalNumbers + " numbers.");
                    for (i = 0; i < totalNumbers; i++) {
                        product *= scan.nextInt();
                    }
                    System.out.println("The product is " + product);
                    System.out.print("Would you like to add or multiply? (Enter stop to be terminate)");
                    choice = scan.next();
                    if (!choice.equalsIgnoreCase("add") || !choice.equalsIgnoreCase("multiply")
                            || !choice.equalsIgnoreCase("stop")) {
                        System.out.println("You entered wrong input");
                        System.exit(0);
                    }
                } else if (choice.equalsIgnoreCase("add")) {
                    sum = 0;
                    System.out.print("How many numbers would you like to add?");
                    totalNumbers = scan.nextInt();
                    System.out.print("Please enter " + totalNumbers + " numbers.");
                    for (i = 0; i < totalNumbers; i++) {
                        sum += scan.nextInt();
                    }
                    System.out.println("The sum is " + sum);
                    System.out.print("Would you like to add or multiply? (Enter stop to be terminate)");
                    choice = scan.next();
                    if (!choice.equalsIgnoreCase("add") || !choice.equalsIgnoreCase("multiply")
                            || !choice.equalsIgnoreCase("stop")) {
                        System.out.println("You entered wrong input");
                        System.exit(0);
                    }
                }
            }
            System.out.print("Come again!");
            scan.close();
        } else {
            System.out.println("You entered wrong input");
        }
    }

you must change scan.nextLine(); to scan.next();

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