清除缓冲区,输出firstname,lastname

时间:2015-04-20 03:29:59

标签: java java.util.scanner

所以我的程序有问题。如果我删除in.nextLine名字和姓氏,println将会一个接一个地打印出来,如果我有它,它将不会打印输出中的第一个名字,因为我认为它正在删除它。任何帮助将不胜感激。

import java.util.*;

public class Main
{

public static void main(String[] args)
{
    //Utilities
    Scanner in = new Scanner(System.in);

    //Variables
    int studentId = 0;
    String lastName = "";
    String firstName = "";
    String gradeLetter = "";
    String quit = "";
    double userHomeworkScore = 0.0;
    double userQuizzesScore = 0.0;
    double userMidTermScore = 0.0;
    double userFinalScore = 0.0;
    double homeworkScore = 0.0;
    double quizzesScore = 0.0;
    double midTermScore = 0.0;
    double finalScore = 0.0;
    double totalScore = 0.0;

    do {


        System.out.println("Please enter Student ID");
        studentId = in.nextInt();

        System.out.println("Please enter first name");
        firstName = in.nextLine();

        in.nextLine();

        System.out.println("Please enter last name");
        lastName = in.nextLine();

        System.out.println("Please enter score for the assignments");
        userHomeworkScore = in.nextDouble();

        System.out.println("Please enter score for the quizzes");
        userQuizzesScore = in.nextDouble();

        System.out.println("Please enter score for the midterm");
        userMidTermScore = in.nextDouble();

        System.out.println("Please enter score for the final");
        userFinalScore = in.nextDouble();

        String fullName = (firstName + "," + lastName);

        homeworkScore = (userHomeworkScore * 0.50);
        quizzesScore = (userQuizzesScore * 0.20);
        midTermScore = (userMidTermScore * 0.10);
        finalScore = (userFinalScore * 0.20);

        totalScore = (homeworkScore+quizzesScore+midTermScore+finalScore);

        if (totalScore >= 90)
        {
            gradeLetter = "A";
        }
        else if (totalScore >= 80)
        {
            gradeLetter = "B";
        }
        else if (totalScore >= 70)
        {
            gradeLetter = "C";
        }
        else if (totalScore >= 60)
        {
            gradeLetter = "D";
        }
        else
        {
            gradeLetter = "E";
        }


        //Output
        System.out.println(""+fullName+"");
        System.out.println("Student ID:" + studentId + "");
        System.out.println("Homework:" + homeworkScore + "");
        System.out.println("Quizzes:" + quizzesScore + "");
        System.out.println("Midterm:" + midTermScore + "");
        System.out.println("Final:" + finalScore + "");
        System.out.println("Total Score: " + totalScore + " Grade: " + gradeLetter + "");

        System.out.println("Press any key to enter another student or enter q to quit.");
        quit = in.nextLine();
        in.nextLine();

    }while(!quit.equalsIgnoreCase("q"));


}

}

1 个答案:

答案 0 :(得分:0)

你需要" Flush"读取整数后的缓冲区。 原因是当你使用in.nextInt()时,它只读取int值并跳过" \ n" (行尾),使用in.nextLine() 将使用" \ n"。

使用此

 System.out.println("Please enter Student ID");
        studentId = in.nextInt();
        in.nextLine();

当然在阅读完最后一个双

之后
System.out.println("Please enter score for the final");
        userFinalScore = in.nextDouble();
        in.nextLine();

你不需要"冲洗"两行读数之间。 这是错的:

System.out.println("Please enter first name");
    firstName = in.nextLine();

    in.nextLine();

    System.out.println("Please enter last name");
    lastName = in.nextLine();

另一个解决方案: 您可以使用in.next()来读取字符串。 in.next()查找并返回扫描仪中的下一个完整令牌。

while in.nextLine(),因为此方法继续搜索输入以查找行分隔符,如果没有行分隔符,它可以缓冲搜索要跳过的行的所有输入。

Scanner in = new Scanner(System.in);

    //Variables
    int studentId = 0;
    String lastName = "";
    String firstName = "";
    String gradeLetter = "";
    String quit = "";
    double userHomeworkScore = 0.0;
    double userQuizzesScore = 0.0;
    double userMidTermScore = 0.0;
    double userFinalScore = 0.0;
    double homeworkScore = 0.0;
    double quizzesScore = 0.0;
    double midTermScore = 0.0;
    double finalScore = 0.0;
    double totalScore = 0.0;

    do {


        System.out.println("Please enter Student ID");
        studentId = in.nextInt();

        System.out.println("Please enter first name");
        firstName = in.next();



        System.out.println("Please enter last name");
        lastName = in.next();


        System.out.println("Please enter score for the assignments");
        userHomeworkScore = in.nextDouble();

        System.out.println("Please enter score for the quizzes");
        userQuizzesScore = in.nextDouble();

        System.out.println("Please enter score for the midterm");
        userMidTermScore = in.nextDouble();

        System.out.println("Please enter score for the final");
        userFinalScore = in.nextDouble();

        String fullName = (firstName + "," + lastName);

        homeworkScore = (userHomeworkScore * 0.50);
        quizzesScore = (userQuizzesScore * 0.20);
        midTermScore = (userMidTermScore * 0.10);
        finalScore = (userFinalScore * 0.20);

        totalScore = (homeworkScore+quizzesScore+midTermScore+finalScore);

        if (totalScore >= 90)
        {
            gradeLetter = "A";
        }
        else if (totalScore >= 80)
        {
            gradeLetter = "B";
        }
        else if (totalScore >= 70)
        {
            gradeLetter = "C";
        }
        else if (totalScore >= 60)
        {
            gradeLetter = "D";
        }
        else
        {
            gradeLetter = "E";
        }



        //Output
        System.out.println(""+fullName+"");
        System.out.println("Student ID:" + studentId + "");
        System.out.println("Homework:" + homeworkScore + "");
        System.out.println("Quizzes:" + quizzesScore + "");
        System.out.println("Midterm:" + midTermScore + "");
        System.out.println("Final:" + finalScore + "");
        System.out.println("Total Score: " + totalScore + " Grade: " + gradeLetter + "");

        System.out.println("Press any key to enter another student or enter q to quit.");
        quit = in.next();

    }while(!quit.equalsIgnoreCase("q"));

输出:

Please enter Student ID
1
Please enter first name
a
Please enter last name
b
Please enter score for the assignments
100
Please enter score for the quizzes
100
Please enter score for the midterm
100
Please enter score for the final
100
a,b
Student ID:1
Homework:50.0
Quizzes:20.0
Midterm:10.0
Final:20.0
Total Score: 100.0 Grade: A
Press any key to enter another student or enter q to quit.
q