更新時(shí)間:2022-12-19 11:28:22 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1466次
1.主方法的形式參數(shù)
2.利用字節(jié)輸入流
3.利用字符輸入流
4.利用文本掃描類
下面將針對(duì)這幾種方法分別利用例子進(jìn)行說(shuō)明:
1. 控制臺(tái)輸入數(shù)據(jù)
(1)主方法的形式參數(shù)
在Java中利用main(String args[])中的args數(shù)組來(lái)對(duì)參數(shù)進(jìn)行賦值,有下例:
package InputTest;
public class DataKeyboardInput1 {
/**主方法的形式參數(shù)來(lái)輸入數(shù)據(jù)
* DataKeyboardInput1.java
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(args[0]);
}
}
在命令行下輸入javac DataKeyboardInput1.java --》java DataKeyboardInput1 HelloWorld 輸出就是 HelloWorld 了
(2)利用字節(jié)輸入流
字節(jié)輸入流System.in 是類InputStream的常量對(duì)象,調(diào)用read()方法可以從鍵盤接收數(shù)據(jù)。實(shí)現(xiàn)的步驟是:先把數(shù)據(jù)讀入字節(jié)數(shù)組中,然后利用字節(jié)數(shù)組定義字符串,最后把字符串轉(zhuǎn)化為需要的數(shù)據(jù)類型。
package InputTest;
import java.io.*;
public class DataKeyboardInput2 {
/**利用字節(jié)輸入流來(lái)輸入數(shù)據(jù)
* DataKeyboardInput2.java
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
byte buf[]=new byte[10]; //字節(jié)數(shù)組,輸入為10個(gè)
String str;
int num = 0;
try{
//把數(shù)據(jù)讀入到字節(jié)數(shù)組中
System.in.read(buf);
//利用字節(jié)數(shù)組創(chuàng)建字符串
str=new String(buf,0);
//把字符串?dāng)?shù)據(jù)轉(zhuǎn)換為整型數(shù)據(jù)
num=Integer.parseInt(str.trim());
}catch(Exception e){
System.out.print(e);
}System.out.println(num);
}
}
(3)利用字符輸入流
字符輸入流BufferedReader是從字符輸入流中讀取文本,緩沖各個(gè)字符,從而提供字符,數(shù)組和行的高效讀取。常用方法:
read(): 讀取單個(gè)字符; readLine():讀取一行字符,即為讀取一個(gè)字符串
package InputTest;
import java.io.*;
public class DataKeyboardInput3 {
/**利用字符輸入流來(lái)進(jìn)行輸入處理
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="";
int num = 0;
try{
str=br.readLine();
num=Integer.parseInt(str);
}catch(IOException e){
System.out.println(e.toString());
}System.out.println(num);
}
}
(4)利用文本掃描類
文本掃描類Scanner是jdk5.0新增加的類,它是一個(gè)可以使用Java正則表達(dá)式來(lái)解析基本類型和字符串的簡(jiǎn)單文本掃描器,它使用分隔符模式將其輸入分解為標(biāo)記默認(rèn)情況下該分隔符模式與空白匹配,然后使用不同的next方法將得到的標(biāo)記轉(zhuǎn)換為不同類型值。獲取基本類型數(shù)據(jù)的方法是:
整型:nextInt() 單精度:nextFloat() 雙精度: nextDouble() 字符串: next()
package InputTest;
import java.util.Scanner;
public class DataKeyboardInput4 {
/**利用文本掃描類來(lái)進(jìn)行輸入處理
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int num=input.nextInt();
System.out.println(num);
}
}
2.對(duì)話框輸入數(shù)據(jù)
使用JOptionPane創(chuàng)建輸入對(duì)話框,實(shí)現(xiàn)鍵盤輸入
showConfirmDialog(): 詢問(wèn)一個(gè)確認(rèn)問(wèn)題,如yes/no/cancer
showInputDialog(): 提示要求某些輸入
showMessageDialog(): 告知用戶某事已經(jīng)發(fā)生
其中,輸入對(duì)話框的常用參數(shù)形式有:
showInputDialog(Object message) : message 表提示信息
showInputDialog(Object message,Object initialSelectionValue): 如果沒(méi)有輸入數(shù)據(jù),則默認(rèn)初始值為initalSlectionValue
package InputTest;
import javax.swing.JOptionPane;
public class DataInputDialog {
/**對(duì)話框輸入數(shù)據(jù)
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str;
str=JOptionPane.showInputDialog("input data:");
int num=Integer.parseInt(str);
System.out.println(num);
}
}
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)