『Java入門第1期』など(Scanner類)
1558 ワード
/*
* Scanner: 。
*
* :
* A:
* B:
* C:
*
* System :
* public static final InputStream in; , 。InputStream si = System.in;
*
* InputStream is = System.in;// InputStream
*
* 。
* class Demo {
* public static final int x = 10;
* public static final Student s = new Student();
* }
* int y = Demo.x;// int
* Student s = Demo.s;// Student 。
* :public static final InputStream in;
* class System{
* public static final InputStream in;
* }
* InputStream is = System.in;// InputStream
* Scanner Scanner(InputStream source)// InputStream。
*
* :
* Scanner(InputStream source)
*/
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
//
Scanner sc = new Scanner(System.in);// , ??
// :System 。in (); , 。 System 。 。
int x = sc.nextInt();
System.out.println("x:" + x);
}
}
/*
* :
* public boolean hasNextXxx(): . : : int true int , true
*
* public Xxx nextXxx():
*
* : int
* public boolean hasNextInt()
* public int nextInt()
*
* :
* InputMismatchException:
*/
注意:キーボードはデータを入力し、まず数値を取得し、文字列を取得すると、問題が発生します.この問題は主に改行によるものです.解決策は文字列で入力し、最後に何が欲しいのか、直接強制的に変換すればいいのが一般的です.Stringクラスには、他の様々なフォーマットデータに変換する方法があるからです.