JAVA第二季6-1綜合練習
Scanner c=new Scanner(System.in); ? ? ? ?int k=c.nextInt(); ? ? ? ?if(k==1)這里為什么不直接用if(c==1)啊,還有nextInt();是什么意思 求大神
Scanner c=new Scanner(System.in); ? ? ? ?int k=c.nextInt(); ? ? ? ?if(k==1)這里為什么不直接用if(c==1)啊,還有nextInt();是什么意思 求大神
2017-03-07
舉報
2017-03-07
因為k才是一個整型值,可以與數(shù)字1做比較。c卻是一個Scanner對象
nextInt()是Scanner類的一個實例方法,用于接收返回用戶在控制臺輸入的整型數(shù)值,具體的在初學階段不用去細究
2017-03-07
Scanner (掃描器):使用分隔符模式將其輸入分解為標記,默認情況下該分隔符模式與空白匹配。
nextInt():讀取從鍵盤輸入的int(數(shù)字)類型的數(shù)據(jù)。可以使用不同的 next 方法將得到的標記轉(zhuǎn)換為不同類型的值。
c 這個只是用來存放掃描從控制臺輸入的字符,通過new Scanner(System.in)創(chuàng)建一個Scanner對象,控制臺會一直等待輸入,直到敲回車鍵結(jié)束,把所輸入的內(nèi)容傳給Scanner,作為掃描對象 ?
掃描器還可以使用不同于空白的分隔符。下面是從一個字符串讀取若干項的例子:
? ? String input = "1 fish 2 fish red fish blue fish";
? ? Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
? ? System.out.println(s.nextInt());
? ? System.out.println(s.nextInt());
? ? System.out.println(s.next());
? ? System.out.println(s.next());
? ? s.close();?
? ? 輸出為:
? ? 1
? ? 2
? ? red
? ? blue?
2017-03-07
樓上說的不錯!現(xiàn)在沒必要去深究這個!目前知道怎么用就好