字符串和int

时间:2015-11-06 18:03:39

标签: java

代码就是这样:

import java.util.Scanner;

公共课Prelab1 {

public static void main(String[] args)
{
    int programcase = 0;
    Scanner inputscan = new Scanner(System.in);

    System.out.print("Enter case number: ");
    programcase = inputscan.nextInt();

    switch(programcase)
    {
    case 1:
        {
            ////////////////////////////////////////////////////////////////////////////////////

            //WRITE THE CODE OF CASE 1 HERE
            Scanner scan = new Scanner(System.in);

            System.out.println("Enter first student's name: ");
            String name1 = scan.nextLine(); 
            System.out.println("Enter " + name1 + "'s first grade: ");
            int firstGrade = scan.nextInt();
            System.out.println("Enter " + name1 + "'s second lab grade: ");
            int secondGrade = scan.nextInt();
            System.out.println("Enter " + name1 + "'s third lab grade: ");
            int thirdGrade = scan.nextInt();
            System.out.println("Enter " + name1 + "'s fourth lab grade: ");
            int fourthGrade = scan.nextInt();

            System.out.println("Enter second student's name: ");
            int name2 = scan.nextInt();
            System.out.println("Enter " + name2 + "'s first lab grade: ");
            int secondfirstGrade = scan.nextInt();
            System.out.println("Enter " + name2 + "'s second lab grade: ");
            int secondsecondGrade = scan.nextInt();
            System.out.println("Enter " + name2 + "'s third lab grade: ");
            int secondthirdGrade = scan.nextInt();
            System.out.println("Enter " + name2 + "'s fourth lab grade: ");
            int secondfourthGrade = scan.nextInt();

            System.out.println("/+CENG 111 Course Student List+" + "\\");
            System.out.println("Name\t\t\t/Lab1\t/Lab2\t/Lab3\t/Lab4");
            System.out.println(name1 + "\t\t\t/" + firstGrade + "\t/" + secondGrade + "\t/" + thirdGrade + "\t/" + fourthGrade);
            System.out.println(name2 + "\t\t\t/" + secondfirstGrade + "\t/" + secondsecondGrade + "\t/" + secondthirdGrade + "\t/" + secondfourthGrade);

            scan.close();

它继续,但它并不重要。当我运行它它顺利但在那里它说: 输入第二个学生姓名: 输入第二个学生实验室成绩:

所以它不等我写下它跳到另一个问题的名字。我怎么能解决这个问题呢?

1 个答案:

答案 0 :(得分:-3)

这是Scanner课程的不当行为。它无法正确清除其缓冲区,并希望所有输入都具有相同的数据类型。

要解决此问题,请改用BufferedReader。

BufferedReader x = new BufferedReader (new InputStreamReader (System.in));

int a = Integer.parseInt (x.readLine());

String n = x.readLine();