重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
通過Scanner實(shí)現(xiàn)鍵盤讀取輸入。
創(chuàng)新互聯(lián)建站主要從事成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)明溪,十余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
Scanner使用正則表達(dá)式來解析基本類型和字符串的簡單文本掃描器。通過Scanner掃描System.in的輸入流,可以獲取到鍵盤信息。
比如:
例如,以下代碼使用戶能夠從鍵盤輸入一個(gè)數(shù):
Scanner?sc?=?new?Scanner(System.in);
int?i?=?sc.nextInt();
使用System.in.read可以讀取鍵盤的輸入,但是一般不會(huì)這樣去操作,可以使用java.util.Scanner來配合System.in來進(jìn)行數(shù)據(jù)的操作,舉例如下:
Scanner in=new Scanner(System.in);
String readLine = in.nextLine(); //讀取鍵盤輸入的一行(以回車換行為結(jié)束輸入)
import?java.io.BufferedReader;
import?java.io.IOException;
import?java.io.InputStreamReader;
/*
*?System.in?標(biāo)準(zhǔn)輸入流。是從鍵盤獲取數(shù)據(jù)的
*?
*?鍵盤錄入數(shù)據(jù):
*? A:main方法的args接收參數(shù)。
*? java?HelloWorld?hello?world?java
*? B:Scanner(JDK5以后的)
*? Scanner?sc?=?new?Scanner(System.in);
*? String?s?=?sc.nextLine();
*? int?x?=?sc.nextInt()
*? C:通過字符緩沖流包裝標(biāo)準(zhǔn)輸入流實(shí)現(xiàn)
*? BufferedReader?br?=?new?BufferedReader(new?InputStreamReader(System.in));
*/
public?class?SystemInDemo?{
public?static?void?main(String[]?args)?throws?IOException?{
//?//獲取標(biāo)準(zhǔn)輸入流
//?InputStream?is?=?System.in;
//?//我要一次獲取一行行不行呢?
//?//行。
//?//怎么實(shí)現(xiàn)呢?
//?//要想實(shí)現(xiàn),首先你得知道一次讀取一行數(shù)據(jù)的方法是哪個(gè)呢?
//?//readLine()
//?//而這個(gè)方法在哪個(gè)類中呢?
//?//BufferedReader
//?//所以,你這次應(yīng)該創(chuàng)建BufferedReader的對象,但是底層還是的使用標(biāo)準(zhǔn)輸入流
//?//?BufferedReader?br?=?new?BufferedReader(is);
//?//按照我們的推想,現(xiàn)在應(yīng)該可以了,但是卻報(bào)錯(cuò)了
//?//原因是:字符緩沖流只能針對字符流操作,而你現(xiàn)在是字節(jié)流,所以不能是用?
//?//那么,我還就想使用了,請大家給我一個(gè)解決方案?
//?//把字節(jié)流轉(zhuǎn)換為字符流,然后在通過字符緩沖流操作
//?InputStreamReader?isr?=?new?InputStreamReader(is);
//?BufferedReader?br=?new?BufferedReader(isr);
BufferedReader?br?=?new?BufferedReader(new?InputStreamReader(System.in));
System.out.println("請輸入一個(gè)字符串:");
String?line?=?br.readLine();
System.out.println("你輸入的字符串是:"?+?line);
System.out.println("請輸入一個(gè)整數(shù):");
//?int?i?=?Integer.parseInt(br.readLine());
line?=?br.readLine();
int?i?=?Integer.parseInt(line);
System.out.println("你輸入的整數(shù)是:"?+?i);
}
}
某個(gè)時(shí)刻讀取? 需要特定時(shí)間? 如果僅僅是輸入很簡單, 比如: Scanner scanner=new Scanner(System.in); System.out.println("請輸入:"); String s = scanner.next(); System.out.println("打印輸入:" + s); 追問: 有個(gè)固定時(shí)間的 回答: 什么時(shí)間呢?具體點(diǎn)吧.. 是倒計(jì)時(shí)還是準(zhǔn)確時(shí)間/.? 追問: 準(zhǔn)確時(shí)間 回答: 做一個(gè)定時(shí)器就可以了哈. 在指定時(shí)間執(zhí)行指定的操作. 追問: 對的說的不錯(cuò) 要實(shí)際點(diǎn) 代碼 回答: public class Test{ public class TimerTest extends TimerTask { public void run() { Scanner scanner=new Scanner(System.in); System.out.println("請輸入:"); String s = scanner.next(); System.out.println("打印輸入:" + s); } } public void RunTest() { TimerTest task = new TimerTest(); Timer timer = new Timer(); Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.roll(Calendar.SECOND, 5); // 當(dāng)前時(shí)間基礎(chǔ)上加上5秒 timer.schedule(task, cal.getTime()); } public static void main(String arg[]) { Test t = new Test(); t.RunTest(); } }