【问题标题】:This particular code is not reading the input given as string 's'. How to correct this? [duplicate]此特定代码未读取作为字符串“s”给出的输入。如何纠正这个? [复制]
【发布时间】:2017-04-04 20:04:10
【问题描述】:
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 s = scan.nextLine();

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

}

【问题讨论】:

  • scan.nextDouble();之后添加scan.nextLine();
  • 你的错误/输出是什么?

标签: java string java.util.scanner stdout stdin


【解决方案1】:

你可以使用

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

请注意,Scanner.nextInt()Scanner.nextDouble 等不会得到最后一行。所以你需要单独调用它们。

【讨论】:

    猜你喜欢
    • 2014-01-09
    • 1970-01-01
    • 2011-08-02
    • 2012-07-20
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多