为什么我的 for 循环在使用 (scanner).nextLine(); 时一次打印 2 个提示?

Why is my for loop printing 2 of my prompts at one time while using (scanner).nextLine();

是我的 while 或 for 循环有问题还是我遗漏了什么?首先 运行 通过工作正常但在第二个我得到这个:

Enter course NAME: class name
Enter course HOURS: 4
Enter course GRADE: 4.0
You have entered class: jeff graves, class hours: 4 and, class grade 4.0
Do you want to continue:
y
Enter course NAME: Enter course HOURS: 

使用 (scanner).next(); 它工作得很好,但是我只能从用户那里获取一个词,当它翻转时它会从 nextInt 中抛出一个错误。

   public class GetGrades { //open class GetGrades

    public static void main(String[] args)  {
/**creating new instance of scanner named input to read user input*/
        Scanner input = new Scanner(System.in);  // create new instance of scanner object
        boolean repeat = true;  //boolean value for while loop holding array input
        String s;  // create string to store user input value for exiting loops
/** create 3 arrays to store course name, hours, and grades*/
        String[] name = new String[20]; //type string array for class name
        int[] hours = new int[20]; // type int array for class hours
        float[] grade = new float[20]; //type float array for class grade


        outerloop:  // set break point for nested for loops exit on user input 
            while(repeat != false)  {// while loop with boolean value to let user exit array input
        for (int i=0; i<name.length; i++)  { //for loop for name array

            System.out.print("Enter course NAME: "); //prompt user for input
            name[i] = input.nextLine(); //read next line value and store in array name
            System.out.print("Enter course HOURS: "); //prompt user for input
            hours[i] = input.nextInt(); //read the next int and store in array hours
            System.out.print("Enter course GRADE: "); //prompt user for input
            grade[i] = input.nextFloat(); //read the next float value and store in array grade

        /**Print line to console summing um what the user has entered*/
            System.out.println("You have entered class: " + name[i] + ", class hours: " + hours[i] + 
                    " and, class grade " + grade[i]);

/**prompt user if wanted to enter more grades, break loop on n or N*/
            System.out.println("Do you want to continue:");
            s = input.next();
            if ( s.equals("y") || s.equals("Y"))  { //open if statement
                repeat = true;
            } else  { //close if and open else
                break outerloop;
            }  //close else statement


          }//close for loop with i as count
        }//close while

input.next() 将读下一个单词。 input.nextLine() 将一直读到您下次按回车键。

这意味着当你写"y"并回车时,你同时输入了一个单词"y",以及下一个回车,同时填写两个提示并导致下一个要写的提示。

当您要求继续时,您只需将 next() 替换为 nextLine()

        System.out.println("Do you want to continue:");
        s = input.next();

变成

        System.out.println("Do you want to continue:");
        s = input.nextLine(); 

从而读取 "y" 和回车。下一个提示现在可以自由接受新输入了。

当你输入等级,例如12.3,然后输入,"input.nextFloat();"只会取“12.3”而不会取"enter",所以"enter"会被下一个扫描器取。

在我看来,

首先,将 "s = input.next()" 更改为 "s = input.nextLine()",但它会占用先前扫描仪 "grade[i] = input.nextFloat();" 的 "enter",因此,其次,将其放入 while 循环中一个条件。像这样

while((s = input.nextLine()).equals("")) {}

因此,它不会停止,直到获得期望的输入。

试试这个..

System.out.print("Do you want to continue:");
while((s = input.nextLine()).equals("")) {}
if (s.equals("y") || s.equals("Y")) { // open if statement
    repeat = true;
} else { // close if and open else
    break outerloop;
} // close else statement