1. 程式人生 > 資訊 >騰訊天美聯合微軟 Xbox 工作室推出 RTS 手遊《重返帝國》,現公佈實機演示

騰訊天美聯合微軟 Xbox 工作室推出 RTS 手遊《重返帝國》,現公佈實機演示

Scanner物件

Scanner s = new Scanner(System.in);

接收使用者輸入的資料next() 一定讀取有效字元才可結束,對輸入有效字元前遇到空白,會自動去掉空白,對輸入有效字元後遇到空白,會將空白作為分隔符或結束符,所以不會得到帶空格的字串 nextLine() 以回車鍵為結束符,可以獲得空白 判斷使用者是否有下一步輸入hasNext() hasNextLine()

建立一個掃描器物件,用於接收鍵盤資料

Scanner scanner = new Scanner(System.in);

System.out.println("使用next方法接收:");

//判斷使用者有沒有輸入字串

if(scanner.hasNext()){

//使用next方法接收

String str = scanner.next(); //程式會等待使用者輸入完畢

System.out.println("輸出內容為:"+str);

}

//凡是屬於IO流(輸入輸出)的類如果不關閉會一直佔用資源,要養成關閉的的習慣

scanner.close();

![圖片](C:\Users\單祥展\Desktop\螢幕截圖 2021-08-19 095130.png)

int i =0;

float f =0.0f

System.out.println("請輸入整數:");

if(scanner.hasNextInt()){

i=scanner.nextInt();

System.out.println("整數資料"+i);

} else{

System.out.println("輸入的不是整數資料");

}

System.out.println("請輸入小數:");

if(scanner.hasNextfloat()){

i=scanner.nextfloat();

System.out.println("小數資料"+f);

} else{

System.out.println("輸入的不是小數資料");

}

double a =0;

int b =0;

while(scanner.hasNextfloat()){

​ double x =scanner.nextDouble();

​ b =b+1

​ a = a+x;

System.out.println("你輸入了第"+b"個數據,當前結果sum=“+a);

System.out.println(b + "個數的和"+a);

System.out.println(b +“個數的平均值”+(a/b);