〖JAVA IO〗_IO操作執行個體筆記
本章目標:
掌握java資料資料的操作執行個體
執行個體一:加法操作
執行個體二:菜單顯示
3.本執行個體主要採用的知識
1、鍵盤輸入資料的標準格式
2、日期的轉換,以及封裝類的使用
3、類的設計思路。
4.具體內容
執行個體一代碼:
import java.io.*;public class ExecDemo01{ public static void main(String args[]) throws Exception{ int i=0; int j=0; BufferedReader buf = null; buf = new BufferedReader(new InputStreamReader(System.in)); String str=null; System.out.println("請輸入第一個數字"); str = buf.readLine(); //接收資料 i = Integer.parseInt(str); System.out.println("請輸入第二個數字"); str = buf.readLine(); //接收資料 j = Integer.parseInt(str); System.out.println(i+"+"+i+"="+(i+j)); }}
以上確實完成了,但是有以下問題:
a.現在資訊是由使用者輸入的,使用者輸入的如果不是數字呢?
b.本程式只能輸入整數
c.代碼重複,一使用輸入資料,則肯定使用BufferedReader,所以代碼重複了。
此時,需要對類進行更加合理的劃分。
輸入資料,最可能的是:整數、小數、日期、字串,所以最好將其設計出一個專門的輸入資料類型,完成輸入資料的功能,而且在此類中還可以對輸入的資料進行驗證。
下面:完成專門處理輸入資料的類,但是只能得到整數和字串。
import java.io.* ;import java.util.* ;import java.text.* ;public class InputData{ private BufferedReader buf = null ; public InputData(){// 只要輸入資料就要使用此語句 this.buf = new BufferedReader(new InputStreamReader(System.in)) ; } public String getString(String info){ // 得到字串資訊 String temp = null ; System.out.print(info) ; // 列印提示資訊 try{ temp = this.buf.readLine() ; // 接收資料 }catch(IOException e){ e.printStackTrace() ; } return temp ; } public int getInt(String info,String err){ // 得到一個整數的輸入資料 int temp = 0 ; String str = null ; boolean flag = true ; // 定義一個標記位 while(flag){ str = this.getString(info) ; // 接收資料 if(str.matches("^\\d+$")){ // 判斷是否由數字組成 temp = Integer.parseInt(str) ; // 轉型 flag = false ; // 結束迴圈 }else{ System.out.println(err) ; // 列印錯誤資訊 } } return temp ; } public float getFloat(String info,String err){ // 得到一個小數的輸入資料 float temp = 0 ; String str = null ; boolean flag = true ; // 定義一個標記位 while(flag){ str = this.getString(info) ; // 接收資料 if(str.matches("^\\d+.?\\d+$")){ // 判斷是否由數字組成 temp = Float.parseFloat(str) ; // 轉型 flag = false ; // 結束迴圈 }else{ System.out.println(err) ; // 列印錯誤資訊 } } return temp ; } public Date getDate(String info,String err){ // 得到一個小數的輸入資料 Date temp = null ; String str = null ; boolean flag = true ; // 定義一個標記位 while(flag){ str = this.getString(info) ; // 接收資料 if(str.matches("^\\d{4}-\\d{2}-\\d{2}$")){ // 判斷是否由數字組成 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ; try{ temp = sdf.parse(str) ; // 將字串變為Date型資料 }catch(Exception e){} flag = false ; // 結束迴圈 }else{ System.out.println(err) ; // 列印錯誤資訊 } } return temp ; }};
測試 InputDate類
import java.io.* ;import java.util.* ;public class TestInput{ public static void main(String args[]) throws Exception{ InputData input = new InputData() ; // float f = input.getFloat("請輸入小數:","輸入的不是小數,請重新輸入!") ; // System.out.println("小數:" + f) ; Date d = input.getDate("請輸入日期,格式為(yyyy-mm-dd):","輸入的日期格式不正確,請重新輸入") ; System.out.println("日期" + d) ; }};
最終代碼:
import java.io.* ;public class ExecDemo01{ public static void main(String args[]) throws Exception{ int i = 0 ; int j = 0 ; BufferedReader buf = null ; // 接收鍵盤的輸入資料 buf = new BufferedReader(new InputStreamReader(System.in)) ; String str = null ; // 接收資料 System.out.print("請輸入第一個數字:") ; str = buf.readLine() ; // 接收資料 i = Integer.parseInt(str) ; // 將字串變為整數 System.out.print("請輸入第二個數字:") ; str = buf.readLine() ; // 接收資料 j = Integer.parseInt(str) ; // 將字 System.out.println(i + " + " + j + " = " + (i + j)) ; }};
4.2、菜單顯示
菜單顯示在各個系統中都是比較常見的功能。
現在是要求一個菜單的顯示,但是實際上每個選項之後都肯定會有自己的若干操作,所以為了方便操作,建立以下幾個類:
Menu(專門顯示菜單資訊) -> Operate(表示操作類)
public class Operate{ public static void add(){ // 增加操作 System.out.println("** 選擇的是增加操作") ; } public static void delete(){ // 刪除操作 System.out.println("** 選擇的是刪除操作") ; } public static void update(){ // 更新操作 System.out.println("** 選擇的是更新操作") ; } public static void find(){ // 查看操作 System.out.println("** 選擇的是查看操作") ; }};
public class Menu{ public Menu(){ while(true){ this.show() ; // 無限制調用菜單的顯示 } } public void show(){ System.out.println("===== Xxx系統 =====") ; System.out.println(" [1]、增加資料") ; System.out.println(" [2]、刪除資料") ; System.out.println(" [3]、修改資料") ; System.out.println(" [4]、查看資料") ; System.out.println(" [0]、系統退出\n") ; InputData input = new InputData() ; int i = input.getInt("請選擇:","請輸入正確的選項!") ; switch(i){ case 1:{ Operate.add() ; // 調用增加操作 break ; } case 2:{ Operate.delete() ; // 調用刪除操作 break ; } case 3:{ Operate.update() ; // 調用更新操作 break ; } case 4:{ Operate.find() ; // 調用查看操作 break ; } case 0:{ System.exit(1) ; // 系統退出 break ; } default:{ System.out.println("請選擇正確的操作!") ; } } }};
import java.io.* ;public class ExecDemo03{ public static void main(String args[]) throws Exception{ new Menu() ; }};