java的四種常用輸入方法你會幾種
java的輸入方法最常見的就是Scanner的方法,我經(jīng)過查閱一些資料發(fā)現(xiàn)了輸入方法原來還有那么多種,可以玩出不少花樣,下面是我總結出的四種輸入方式,有需要的可以拿去
1.Scanner相關的功能
Scanner的輸入方法是最常見的一種,也是小編在此最推薦的一種,固定格式如下:
import java.util.Scanner; public class TestDemo1007_4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); System.out.println(a); } }
而這里的“int a = scanner.nextInt();"表示從鍵盤輸入的是一個整數(shù),但如果想輸入字符串就需要下面這樣寫:
Scanner scanner = new Scanner(System.in); String a = scanner.next(); System.out.println(a);
這個時候我們想要輸入字符串,就可以順利讀出了,這是運行結果:
但是,如果我們想一次性讀取多個字符串,就像這樣輸入,我們發(fā)現(xiàn)了問題:
輸入多個字符串之后,只輸出了第一個空格之前的字符,一旦遇到了空格就停止讀取了,那么如果在這種情景下,scanner.next()就行不通了,需要用:
Scanner scanner = new Scanner(System.in); String a = scanner.nextLine(); System.out.println(a);
這樣問題就解決了,可以順利輸出全部字符。
2.System相關的功能
個人認為這種輸入方法與上面的方法比起來可能是少了一些存在的必要性,但是存在就一定有合理之處,下面是幾種使用方法:
- 解釋一
public static void main(String[] args) throws IOException { char c; System.out.print("Please input a char:"); c = (char) System.in.read(); //從標準輸入讀入u一個字節(jié)的信息,并返回一盒字符型變量 System.out.println("Receive char =" + c); }
(值得注意的是:當使用System下的read方法來進行輸入時,需要處理一個異常,否則會飄紅。)
以上代碼可以從鍵盤中讀取一個字符,但是只能讀取第一個,不管輸入多少,只能讀取第一個,輸出結果如下圖:
2.解釋二
使用這種方法進行輸入時,會因為你的鍵盤輸入習慣等問題對結果造成影響,而且,返回值始終為ASCⅡ碼(有點頭疼?。?/p>
public static void main(String[] args) throws IOException { System.out.println("請輸入:"); int i =0; while (true){ i = System.in.read(); System.out.println(i); } }
以上代碼的運行情況如下:
所以就像輸出結果說的,我們的空格。回車等操作也會被翻譯成ASCⅡ碼打印出來。
- 下面是計算輸入了多少個字符的代碼;
public static void main(String[] args) throws IOException { byte[] b = new byte[5]; while (true){ int len = System.in.read(b); System.out.println("讀取字節(jié)數(shù):" + len); for (int i = 0; i < len ; i++) { System.out.print(b[i] + "\t"); //返回的是字節(jié)數(shù),由于read的特性,如果輸入12加(空格)加(回車)那就是四個字符, } } }
輸出結果為:
3.使用命令行相關設置
說白了就是使用以下語句:
DataInputStream din = new DataInputStream(System.in);
和Scanner的用法差不多,下面是它的具體用法,代碼如下:
public static void main4(String[] args){ double x = 0, y = 0; DataInputStream din = new DataInputStream(System.in); try { System.out.println("輸入x的數(shù)值:"); x = Double.parseDouble(din.readLine()); System.out.println("輸入y的數(shù)值:"); y = Double.parseDouble(din.readLine()); System.out.println("x的數(shù)值:" + x + "; y的數(shù)值:" + y); System.out.println("x + y = "+ (x+y)); }catch (Exception e){ System.out.println("錯誤??!"); } }
輸出結果如下:
4.JOptionPane相關功能
這一種輸入方法和之前三種輸入輸出的形式都有所不同,他是會在執(zhí)行操作的時候,彈出一個彈框,所有的輸入輸出都需要從彈框中輸入顯示。
1.顯示輸入消息框,可以輸入數(shù)據(jù)
String str1 = JOptionPane.showInputDialog(“輸入消息框”,“0”);
2.顯示出一個彈框
null表示對話框顯示在屏幕中間
第二個參數(shù)表示要顯示的字符結果
JOptionPane.showMessageDialog(null,str1);
JOptionPane.showMessageDialog(null,“a + b =” + c);
一個很簡單的代碼,用來做加減乘除運算:
public static void main(String[] args) { double a,b; String str1 = JOptionPane.showInputDialog("輸入a的值","0"); //由于這個方法輸入的格式為字符型,所以我們要轉(zhuǎn)換成整型 a = Integer.parseInt(str1); String str2 = JOptionPane.showInputDialog("輸入運算符號","+"); String str3 = JOptionPane.showInputDialog("輸入b的值","0"); b = Integer.parseInt(str3); double c = 0; if (str2.equals("+") ){ c = a + b; } if (str2.equals("-")){ //或者是str.contains("-") c = a - b; } if (str2.equals("*") ){ c = a * b; } if (str2.equals("/") ){ c = a / b; } JOptionPane.showMessageDialog(null, c); }
輸出如下:
這種輸入和輸出格式很新穎,很快引起了小編的注意,但這種方法也存在他自己的局限性
綜上:最建議使用的方法還是Scanner
5.應用
那么我將利用四種輸入方法編寫一個很簡單的程序:
實現(xiàn)三個數(shù)排序、最大值、最小值、平均值的計算
(由于只有主函數(shù)部分有差異,所以下面是四種主函數(shù)的寫法,其余的函數(shù)在后面有附上)
- 方法一:Scanner
public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); System.out.println("請輸入三個數(shù):"); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); int[] order = OrderNum(a,b,c); int max = MaxNum(a,b,c); int min = MinNum(a,b,c); double ave = AveNum(a,b,c); System.out.println("最大值為:" + max); System.out.println("最小值為:" + min); System.out.println("平均值為:" + ave); System.out.println("由大到小的順序為;" + Arrays.toString(order)); }
- 方法二:System
public static void main(String[] args) throws IOException { System.out.println("請輸入三個數(shù):"); int a = System.in.read(); int b = System.in.read(); int c = System.in.read(); int[] order = OrderNum(a,b,c); int max = MaxNum(a,b,c); int min = MinNum(a,b,c); double ave = AveNum(a,b,c); System.out.println("最大值為:" + (max-48)); System.out.println("最小值為:" + (min-48)); System.out.println("平均值為:" + (ave-48)); System.out.println("由大到小的順序為;" + Arrays.toString(order)); }
- 方法三:命令行
public static void main(String[] args) throws IOException { DataInputStream din = new DataInputStream(System.in); System.out.println("請輸入三個數(shù):"); int a = Integer.parseInt(din.readLine()); int b = Integer.parseInt(din.readLine()); int c = Integer.parseInt(din.readLine()); int[] order = OrderNum(a,b,c); int max = MaxNum(a,b,c); int min = MinNum(a,b,c); double ave = AveNum(a,b,c); System.out.println("最大值為:" + max); System.out.println("最小值為:" + min); System.out.println("平均值為:" + ave); System.out.println("由大到小的順序為;" + Arrays.toString(order)); }
- 方法四:JOptionPane
public static void main(String[] args) { System.out.println("請在對話框中輸入三個數(shù):"); String str1 = JOptionPane.showInputDialog("輸入第一個數(shù)的值","0"); int a = Integer.parseInt(str1); String str2 = JOptionPane.showInputDialog("輸入第二個數(shù)的值","0"); int b = Integer.parseInt(str2); String str3 = JOptionPane.showInputDialog("輸入第三個數(shù)的值","0"); int c = Integer.parseInt(str3); int[] order = OrderNum(a,b,c); int max = MaxNum(a,b,c); int min = MinNum(a,b,c); double ave = AveNum(a,b,c); JOptionPane.showMessageDialog(null, "三個數(shù)的最大值為:" + max); JOptionPane.showMessageDialog(null, "三個數(shù)的最小值為:" + min); JOptionPane.showMessageDialog(null, "三個數(shù)的平均值為:" + ave); JOptionPane.showMessageDialog(null, "三個數(shù)由大到小為:" + Arrays.toString(order)); }
附:下面是公用函數(shù)部分:
private static int[] OrderNum(int a, int b, int c) { int tmp = 0; if (a < b){ tmp = a; a = b; b = tmp; } if (a < c){ tmp = a; a = c; c = tmp; } if (b < c){ tmp = b; b = c; c = tmp; } int[] nums ={a, b, c}; return nums; } private static int MaxNum(int a, int b, int c) { int tmp = 0; if (a < b){ tmp = a; a = b; b = tmp; } if (a < c){ tmp = a; a = c; c = tmp; } if (b < c){ tmp = b; b = c; c = tmp; } return a; } private static int MinNum(int a, int b, int c) { int tmp = 0; if (a < b){ tmp = a; a = b; b = tmp; } if (a < c){ tmp = a; a = c; c = tmp; } if (b < c){ tmp = b; b = c; c = tmp; } return c; } private static double AveNum(int a, int b, int c) { int sum = a + b + c; return (double)sum/3; }
總結
到此這篇關于java四種常用輸入方法的文章就介紹到這了,更多相關java輸入方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot+mybatis-plus實現(xiàn)自動建表的示例
本文主要介紹了springboot+mybatis-plus實現(xiàn)自動建表的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-06-06SpringBoot實現(xiàn)文件的上傳、下載和預覽功能
在Spring Boot項目中實現(xiàn)文件的上傳、下載和預覽功能,可以通過使用Spring MVC的MultipartFile接口來處理文件上傳,并使用HttpServletResponse或Resource來實現(xiàn)文件下載和預覽,下面是如何實現(xiàn)這些功能的完整示例,需要的朋友可以參考下2024-08-08Java IO學習之緩沖輸入流(BufferedInputStream)
這篇文章主要介紹了Java IO學習之緩沖輸入流(BufferedInputStream)的相關資料,需要的朋友可以參考下2017-02-02SpringBoot-RestTemplate如何實現(xiàn)調(diào)用第三方API
這篇文章主要介紹了SpringBoot-RestTemplate實現(xiàn)調(diào)用第三方API的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08