|
Java FAQ
|
| キーボードから読み込んだ値を変数に格納するにはどうすればよいでしょうか。 |
各部分の概要は、以下のとおりです。import java.util.Scanner; // [a] class A { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); // [b] stdIn.nextInt() // [c] } }
キーボードと結び付いた標準入力ストリームSystem.inから文字や数値を取り出す《抽出装置》を表すための変数がstdInです。stdInという変数名は、他の名前に変更しても構いません(その場合は、プログラム中のすべてのstdInを変更することになります)。int x = stdIn.nextInt();
| メソッド | 型 | 読み込む値 |
| nextBoolean() | boolean | trueまたはfalse |
| nextByte() | byte | -128 ~ +127 |
| nextShort() | short | -32768 ~ +32767 |
| nextInt() | int | -2147483648 ~ +2147483647 |
| nextLong() | long | -9223372036854775808 ~+9223372036854775807 |
| nextFloat() | float | ±3.40282347E+38 ~ ±1.40239846E-45 |
| nextDouble() | double | ±1.79769313486231507E+378 ~ ±4.94065645841246544E-324 |
| next() | String | 文字列(スペース・改行などで区切られる) |
| nextLine() | String | 1行分の文字列 |