如何從鍵盤讀值 並 output到command window上
// 記得要import Scanner
import java.util.Scanner
public class IO
{
public static void main(String args []){
Scanner input = new Scanner(System.in);
int i = input.nextInt();
System.out.println(i);}
}
整理一些Scanner的constructor和method
constructor
public Scanner(File source) throws FileNotFoundException |
例:作file的輸入 |
public Scanner(String source) |
例:作字串的輸入 |
public Scanner(InputStream source) |
例:作基本資料型態的輸入 作為參數的 System.in 相當於 The "standard" input stream |
method
boolean hasNextDouble() |
當有下一個Double值輸入時,回傳true Double可被換成 Boolean Byte float int line long short 或是直接只有hasNext() |
Double nextDouble() |
回傳輸入的double值 Double可被換成 Boolean Byte float int line long short 或是直接只有next() |