【问题标题】:Exception Thread in javajava中的异常线程
【发布时间】:2018-04-24 16:21:13
【问题描述】:

我有一个“线程异常”错误。我创建了一个公共类student 并给它三个类:学生姓名、学生 ID 和学生 GPA。当我运行代码并为 student1 输入信息时,这很好。但是当我输入 student2 时,代码会跳过 student2 的名称,并给我学生 ID 和 GPA。我该如何解决这个问题?

代码如下:

import java.util.Scanner;
class Student {

    public String name;
    public int id;
    public float gpa;
}
public class learning {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        Student s1 = new Student();
        System.out.print("Enter your name: ");
        s1.name = input.nextLine();
        System.out.print("Enter your id: ");
        s1.id = input.nextInt();
        System.out.print("Enter your gpa: ");
        s1.gpa = input.nextFloat();


        Student s2 = new Student();
        System.out.print("Enter your name: ");
        s2.name = input.nextLine();
        System.out.print("Enter your id: ");
        s2.id = input.nextInt();
        System.out.print("Enter your gpa: ");
        s2.gpa = input.nextFloat();


        Student s3 = new Student();
        System.out.print("Enter your name: ");
        s3.name = input.nextLine();
        System.out.print("Enter your id: ");
        s3.id = input.nextInt();
        System.out.print("Enter your gpa: ");
        s3.gpa = input.nextFloat();

        System.out.println("your name: " + s1.name + "\n"
                + "your id: " + s1.id + "\n"
                + "your GPA: " + s1.gpa);

    }

}

【问题讨论】:

  • 欢迎来到 Stackoverflow,请阅读How To Ask。特别关注How To Create MCVE。您在发布一个好问题上付出的努力越多:一个易于阅读、理解并且是 on topic 的问题 - 它吸引相关人员的机会就越高,您将更快地获得帮助。祝你好运!
  • 显示你的代码让我们弄清楚发生了什么
  • 请在这里查看pastebin.com/grCEuM9w
  • 使用edit 功能并在问题内提供代码,而不是在某些外部网站上。
  • 你仍然缺少任何类型的问题描述。

标签: java exception


【解决方案1】:

如果您同时使用nextLinenext/nextInt/nextDouble,您的扫描仪就会搞砸了,请参阅this 了解更多信息。

我相应地更改了您的主要方法,它现在可以按照您的意愿工作。

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    Student s1 = new Student();
    System.out.print("Enter your name: ");
    s1.name = input.nextLine();
    System.out.print("Enter your id: ");
    s1.id = Integer.parseInt(input.nextLine());
    System.out.print("Enter your gpa: ");
    s1.gpa = Float.parseFloat(input.nextLine());

    Student s2 = new Student();
    System.out.print("Enter your name: ");
    s2.name = input.nextLine();
    System.out.print("Enter your id: ");
    s2.id = Integer.parseInt(input.nextLine());
    System.out.print("Enter your gpa: ");
    s2.gpa = Float.parseFloat(input.nextLine());

    Student s3 = new Student();
    System.out.print("Enter your name: ");
    s3.name = input.nextLine();
    System.out.print("Enter your id: ");
    s3.id = Integer.parseInt(input.nextLine());
    System.out.print("Enter your gpa: ");
    s3.gpa = Float.parseFloat(input.nextLine());

    System.out.println("your name: " + s1.name + "\n" + "your id: " + s1.id + "\n" + "your GPA: " + s1.gpa);

}

【讨论】:

    猜你喜欢
    • 2018-06-16
    • 2011-10-03
    • 2013-03-13
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2012-01-13
    • 2012-10-23
    相关资源
    最近更新 更多