字符串行未在输出屏幕中打印。错误

时间:2019-08-01 13:41:34

标签: java

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        double d= scan.nextDouble();
        String a= scan.next();
        // Write your code here.

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

1 个答案:

答案 0 :(得分:0)

nextDouble ()本身不会消耗新行,因此返回的下一个标记将是一个空字符串。因此,您需要对其进行扫描。 nextLine ()。

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    System.out.print("Insert an int: ");
    int i = scan.nextInt();

    System.out.print("Insert a double: ");
    double d = scan.nextDouble();
    scan.nextLine();

    System.out.print("Insert a string: ");
    String a = scan.nextLine();

    System.out.println("Inserted string: " + a);
    System.out.println("Inserted double: " + d);
    System.out.println("Inserted int: " + i);
}

,输出为:

Insert an int: 45
Insert a double: 7.7452
Insert a string: Hello World

Inserted string: Hello World
Inserted double: 7.7452
Inserted int: 45