提问人:dissidenttux 提问时间:5/31/2022 最后编辑:dani-vtadissidenttux 更新时间:5/31/2022 访问量:443
为什么我必须输入两个重复的 nextLine 方法才能让空格处理打印 String 而不是只有一个重复的 nextLine 方法?
Why do I have to input two duplicate nextLine method in order for spaces to work on printing a String instead of just one duplicate nextLine method?
问:
我正在接受一个编码挑战,在这个挑战中,我要从中取出一个整数、一个双精度和一个字符串,并将此打印在 .String 必须是一个 String,其中必须包含多个单数单词,并用空格分隔。stdin
stdout
当我只用 行下方的单数方法输入代码时,它在挑战网站上有效,但在我的编辑器中不起作用。网站和我的编辑都使用 Java 8。我尝试在编辑器中切换不同的 Java 8 包,但这并没有区别。什么给了?nextLine
nextDouble
通常,从我收集到的信息来看,您只需要放置一个虚拟人即可解决 Java 方法不读取通过按“Enter”创建的换行符的问题。但在这种情况下,我必须这样做两次。nextLine
nextInt
import java.util.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
// Write your code here.
scan.nextLine();
double d = scan.nextDouble();
scan.nextLine();
scan.nextLine(); //why do I have to include this line in order for spaces to print on my string in stdout?
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
答: 暂无答案
评论
scan.nextLine()
String
double
double
String
scan.nextLine()