Java IO--IO操作實例:加法操作、菜單顯示
實例一:加法操作

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)) ; } };

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 ; } };調用InputData完成加法:
import java.io.* ; public class ExecDemo02{ public static void main(String args[]) throws Exception{ int i = 0 ; int j = 0 ; InputData input = new InputData() ; i = input.getInt("請輸入第一個數字:","輸入的數據必須是數字,請重新輸入!") ; j = input.getInt("請輸入第二個數字:","輸入的數據必須是數字,請重新輸入!") ; System.out.println(i + " + " + j + " = " + (i + j)) ; } };測試類:
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) ; } };
實例二:菜單顯示

菜單顯示在各個係統中都是比較常見的功能,為了方便操作,建立以下幾個類:Menu(專門顯示菜單信息)--->Operate(表示操作類)
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("** 選擇的是查看操作") ; } };
Menu類:顯示的時候因為每一個操作完成之後都應該吧菜單顯示出來,所以肯定是循環調用的過程。
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() ; } };
最後更新:2017-04-03 14:54:08