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();
// scan.nextLine(); **Why should I write this ??**
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
nextLine()
のJavadocによる
:
進歩現在の行過去このスキャナと がスキップされたことの入力を返します。このメソッドは、最後の行区切り文字を除いて、現在の行の残りの部分を返します。 位置は、 の次の行の先頭に設定されます。
ダブル値を入力した後、nextLine()
は、ダブル数値を含む行の余りを読み取ります。そのため、入力文字列には別のnextLine()
が必要です。 next()
を使用して
はあなたの問題を解決するだろう:
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s = scan.next();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
これは、入力文字列として改行文字がかかるため。 –
あなたがダブルを挿入すると、あなたは何の数字を聞いているのですか? –
next()、nextInt()またはnextFoo()メソッドを使用した後に[スキャナがnextLine()をスキップしている可能性があります](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after- using-next-nextint-or-other-nextfoo) –