Residual Network和Inception Network網路架構介紹
阿新 • • 發佈:2020-08-14
-
作用:獲取使用者輸入的資訊
-
工具包:java.util.scanner
-
基本語法:
Scanners=newScanner(System.in);
-
通過Scanner類的next()與nextLine()方法獲取輸入的字串。在讀取前我們一般會使用hasNext()、hasNextLine()判斷是否還有輸入的資料。
程式碼示例
-
用next()方式接收輸入資訊(不能接收完整的輸入資訊,空格之後的資訊不再接收)
publicstaticvoidmain(String[]args) {
// 建立一個掃描器物件,用於接收鍵盤資料
Scannerscanner=newScanner(System.in);
System.out.println("用next()方式接收輸入資訊:");
// 判斷使用者有沒有輸入字串
if(scanner.hasNext()) {
Stringstring=scanner.next();
System.out.println("輸入的內容為:"+string);
}
// 凡是屬於IO流的類如果不關閉會一直佔用資源,故用完就要關掉。
scanner.close();
}
-
用nextLine()方式接收輸入資訊(可以接收完整的輸入資訊)推薦使用 Enter鍵為結束符
publicstaticvoidmain(String[]args) {
// 建立一個掃描器物件,用於接收鍵盤資料
Scannerscanner=newScanner(System.in);
System.out.println("用nextLine()方式接收輸入資訊:");
// 判斷使用者有沒有輸入字串
if(scanner.hasNextLine()) {
Stringstring=scanner.nextLine();
System.out.println("輸入的內容為:"+string);
}
// 凡是屬於IO流的類如果不關閉會一直佔用資源,故用完就要關掉。
scanner.close();
} -
Scanner練習題
publicstaticvoidmain(String[]args) {
//練習題:輸入多個數字,並求其總和與平均數。每輸入一個數字用回車鍵確認,通過輸入非數字來結束輸入並輸出執行結果
// 和
doublesum=0.0;
// 計算輸入多少個數字
intm=0;
Scannerscanner=newScanner(System.in);
System.out.println("請輸入數字:");
while(scanner.hasNextDouble()) {
doublex=scanner.nextDouble();
m++;
sum=sum+x;
System.out.println("你輸入了第"+m+"資料,然後當前結果sum="+sum);
}
System.out.println(m+"個數的和為:"+sum);
System.out.println(m+"個數的平均值為:"+(sum/m));
scanner.close();
}