Java 我试着抓住双打,但没有成功;行不通

Java 我试着抓住双打,但没有成功;行不通,java,Java,您的代码可以更改为以下内容(请记住,关闭扫描仪始终是一个好主意): 这是因为当您输入一个数字并按enter键时,scan.nextInt()只使用输入的数字,而不是“行尾”。当执行scan.nextLine()时,它将使用在执行scan.nextLine()期间提供的第一个输入中仍在缓冲区中的“行尾” 相反,在scan.nextLine()之后立即使用scan.nextLine() 在当前场景中,您将获得异常 public static void main(String[] args) {

您的代码可以更改为以下内容(请记住,关闭扫描仪始终是一个好主意):


这是因为当您输入一个数字并按enter键时,
scan.nextInt()
只使用输入的数字,而不是“行尾”。当执行
scan.nextLine()
时,它将使用在执行
scan.nextLine()
期间提供的第一个输入中仍在缓冲区中的“行尾”

相反,在
scan.nextLine()
之后立即使用
scan.nextLine()

在当前场景中,您将获得异常

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

    String s = scan.nextLine();
    int i = scan.nextInt();
    double d = scan.nextDouble();

    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
    scan.close();
}
您修改的代码将如下所示:

java.lang.NumberFormatException: empty String

如果你是新来的,那就太好了。我不知道如何格式化代码。抱歉,请说出发生了什么与预期发生了什么。请看,在你提供代码并准确描述问题之前,我们无法有效地帮助你,即,你对输入/输出给出/预期了什么。
java.lang.NumberFormatException: empty String
public static void main(String args[])


    {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        scan.nextLine();
        // double d=scan.nextDouble();
        // Write your code here.

        Double d = 0.0;

        try {

            d = Double.parseDouble(scan.nextLine());

        } catch (NumberFormatException e) {

            e.printStackTrace();

    }

        String s = scan.nextLine();

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }