Java Scanner的使用和hasNextXXX()的用法說明
輸入輸出
輸出
基本語法
System.out.println(msg); //輸出一個字符串,自帶換行 System.out.print(msg); //輸出一個字符串,不帶換行 System.out.printf(msg); //格式化輸出,和C語言相同
例如:
public class SannerDemo { public static void main(String[] args) { System.out.println("hello world!"); System.out.print("hello world!"); String str = "hello world"; System.out.printf("%s\n",str); } }
快捷鍵推薦:在這里,如果使用的是 IDEA的話,可以輸入sout然后回車,會自動輸出 System.out.println();
輸入
使用Scanner讀取
首先需要導入==import java.util.Scanner;==的包,然后 Scanner sc =new Scanner(System.in);,這段代碼的主要作用是,從鍵盤中輸入中讀取數(shù)據(jù)。
然后讀取數(shù)據(jù):
next()、nextInt()和nextLIne()的區(qū)別;
import java.util.Scanner; public class SannerDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i = sc.nextInt(); System.out.println(i); //讀取int型的數(shù)據(jù) //讀取一行數(shù)據(jù) String s1 = sc.nextLine(); System.out.println(s1); //讀取字符串 String s2 = sc.next(); System.out.println(s2); }
nextInt():
int i = sc.nextInt(); System.out.println(i); //讀取int型的數(shù)據(jù)
可以讀取數(shù)字,但是遇到空格,只能讀取空格前的數(shù)字。
next():
// //讀取字符串 String s2 = sc.next(); System.out.println(s2);
可以讀取字符串,但是遇到空格,只能讀取空格前的數(shù)字。
nextLine():
//讀取一行數(shù)據(jù) String s1 = sc.nextLine(); System.out.println(s1);
可以讀取字符串,并讀取這一行 ,但是遇到回車結(jié)束。
注意:
next()和nextLine()不可以同時使用:
例如:
//讀取字符串 String s2 = sc.next(); System.out.println(s2); //讀取一行數(shù)據(jù) String s1 = sc.nextLine(); System.out.println(s1);
這樣只會輸出一行,這是因為nextLine()讀取了回車,然后結(jié)束。
next()遇到空客會結(jié)束。
使用Scanner循環(huán)讀取N個數(shù)字/字符串
hasNextInt()的使用
import java.util.Scanner; public class SannerDemo { public static void main(String[] args) { Scanner sc =new Scanner(System.in); while (sc.hasNextInt()){ int i = sc.nextInt();//輸入數(shù)字i System.out.println(i);//打印數(shù)字i } }
當程序開始之后,會一直循環(huán)輸入并打印一個數(shù)字,知道Ctrl+d結(jié)束程序
在這里sc.hasNextInt()的結(jié)果是一個boolean的類型,當結(jié)果為false是結(jié)束。
注意:
Ctrl+d用來結(jié)束循環(huán)輸入多個數(shù)據(jù)
同理:
這些方法都可以用于循環(huán)數(shù)據(jù)輸入。
關(guān)于Scanner中nextxxx()須注意的一點
public static void main(String[] args) { // TODO code application logic here Scanner s = new Scanner(System.in); //需要注意的是,如果在通過nextInt()讀取了整數(shù)后,再接著讀取字符串,讀出來的是回車換行:"\r\n",因為nextInt僅僅讀取數(shù)字信息,而不會讀走回車換行"\r\n". //所以,如果在業(yè)務(wù)上需要讀取了整數(shù)后,接著讀取字符串,那么就應(yīng)該連續(xù)執(zhí)行兩次nextLine(),第一次是取走整數(shù),第二次才是讀取真正的字符串 int i = s.nextInt(); System.out.println("讀取的整數(shù)是"+ i); String rn = s.nextLine();//讀取到的是空格 String a = s.nextLine();//讀取到的是字符串 System.out.println("讀取的字符串是:"+a); }
PS:nextDouble,nextFloat與nextInt是一樣的
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java Web實現(xiàn)添加定時任務(wù)的方法示例
這篇文章主要介紹了Java Web實現(xiàn)添加定時任務(wù)的方法,涉及java web定時任務(wù)控制類定義、調(diào)用及監(jiān)聽器定義、添加等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01SpringCloud-Hystrix-Dashboard客戶端服務(wù)監(jiān)控的實現(xiàn)方法
這篇文章主要介紹了SpringCloud-Hystrix-Dashboard客戶端服務(wù)監(jiān)控的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03Java多線程并發(fā)開發(fā)之DelayQueue使用示例
這篇文章主要為大家詳細介紹了Java多線程并發(fā)開發(fā)之DelayQueue使用示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09Java?spring?boot發(fā)送郵箱實現(xiàn)過程記錄
我們在?站上注冊賬號的時候?般需要獲取驗證碼,?這個驗證碼?般發(fā)送在你的?機號上還有的是發(fā)送在你的郵箱中,這篇文章主要給大家介紹了關(guān)于Java?spring?boot發(fā)送郵箱實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2024-01-01