JAVA實驗5-流_第1頁
JAVA實驗5-流_第2頁
JAVA實驗5-流_第3頁
JAVA實驗5-流_第4頁
JAVA實驗5-流_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、1. 編寫程序,要求:用戶在鍵盤每輸入一行文本,程序?qū)⑦@段文本顯示在控制臺中。當用戶輸入的一行文本是“exit ” (不區(qū)分大小寫)時,程序?qū)⒂脩羲休斎氲奈谋径紝懭氲轿募校⑼顺觥?要求:控制臺輸入通過流封裝獲取,不要使用Scanner)package shiyanwu1;importimportimportpublic class test1 quals(line)|"EXIT".equals(line) break;(line).append(LINE_SEP);esolveOrderDatas(dishesDatas);T T T件1件件1件錄文錄儀儀J 錄文目T

2、目T T件 EL TT 一一 一 一文if (temp20) != null) String dishesName = null;int dishesCount = 0;int totalPrice = 0;forString, Integere : () dishesName =();dishesCount =();totalPrice += (dishesName) * dishesCount;"總消費為:"+totalPrice);private List<String> readFile(String fileName) if (fileName !=

3、null && !"".equals(fileName) File file = null;file = new File(fileName);if () List<String> datas = new ArrayList<String>();try Inputstream is = new FilelnputStream(file);BufferedReader br = new BufferedReader(new InputStreamReader(is,"gb2312");String str = nul

4、l;while (true) str = ();if (str != null) (str); else break;(); catch (Exception e) return datas;return null;private Map<String, Integer> resolveOrderDatas(List<String> datas) String temp1 = null, temp2 = null;String detailStr = null;Map<String, Integer> orderDetail = new HashMap<

5、;>();for (int i = 0; i < (); i+) temp1 = (i).split(",");for (int j = 0; j < ; j+) temp2 = temp1j.split(":");if = 2) (temp20, (temp21)+ (temp20); else (temp20, (temp21);return orderDetail;private Map<String, Integer> resolveDishesDatas(List<String> datas) Map&

6、lt;String, Integer> dishesDetail = new HashMap<>();String temp = null;for (int i = 0; i < (); i+) temp = (i).split(":");if = 2) (temp0, (temp1);return dishesDetail;2. 設計學生類Student ,屬性:學號(整型);姓名(字符串),選修課程(名稱)及課程成績(整型)。編寫一個控制臺程序,能夠?qū)崿F(xiàn)Student 信息的保存、讀取。具體要求:(1)提供 Student 信息的保存功能:通過控

7、制臺輸入若干個學生的學號、姓名以及每個學生所修課程的課程名和成績,將其信息保存到中;(2)數(shù)據(jù)讀取顯示:能夠從文件中讀取學生及其課程成績并顯示于控制臺。package shiyanwu4;public class Student private int number;private String name;private String courseName;private int score;public int getNumber() return number;public void setNumber(int number) = number;public String getName(

8、) return name;public void setName(String name) = name;public String getCourseName() return courseName;public void setCourseName(String courseName) = courseName;public int getScore() return score;public void setScore(int score) =score;package shiyanwu4;import class StudentMgr public static final Stri

9、ng studentDat = "E:/"public static void main(String口 args) showMenu();Scanner s = null;s = new Scanner;String code = null;Student student;List<Student> datas = new ArrayList<Student>();List<Student> savedStudents = readStudentDat(studentDat); while (true) code =();if (&qu

10、ot;#4".equalsIgnoreCase(code) ”程序已退出");break; else if ("#1".equalsIgnoreCase(code) String tmpStr = null;int tmpInt;while (true) " 學生學號:");tmpInt = ();student = new Student();(tmpInt);" 學生姓名:");tmpStr = ();(tmpStr);" 學生課程:");tmpStr = ();(tmpStr);"

11、; 課程成績:");tmpInt = ();(tmpInt);(student);" 輸入 exit 結(jié)束信息錄入, 輸入其他繼續(xù)錄入");tmpStr = ();if ("exit".equalsIgnoreCase(tmpStr) break;if ("exit".equalsIgnoreCase(tmpStr) showMenu();continue; else if ("#3".equalsIgnoreCase(code) try if() >0 )saveStudents(datas);e

12、lse" 無可保存的學生信息"); catch (IOException e) " 保存學生信息異常");();else if ("#2".equalsIgnoreCase(code) List<Student> students = readStudentDat(studentDat);if(students = null | () = 0)" 暫無學生信息");showMenu();else" 已有學生人數(shù):"+();for(int i=0;i<();i+)quals(fi

13、leName) File file = null;file = new File(fileName);Student student = null;if () List<Student> datas = new ArrayList<Student>();try InputStream is = new FileInputStream(file);BufferedReader br = new BufferedReader(new InputStreamReader(is, "gb2312");String str = null;String info

14、s = null;while (true) str = ();if (str != null) student = new Student();str = ();infos = ("#");(infos0);(infos1);(infos2);(infos3);(student); else break;(); catch (Exception e) ();return datas;return null;public static void saveStudents(List<Student> students) throws IOException File

15、 file = new File(studentDat);if (!() ();BufferedWriter bw = new BufferedWriter(new FileWriter(file,true);StringBuffer sb = new StringBuffer();Student s = null;for (int i = 0; i < (); i+) s = (i);(0);() + "#" + () + "#"+ () + "#" + ();();("n");();();public s

16、tatic void showMenu() "");#1、錄入學生信息");#2、查看學生信息");"#3 、保存學生信息");"#4 、退出 ");"");5. 編寫程序,在控制臺窗口提示輸入兩個整數(shù),然后接收這兩個整數(shù),并輸出它們的和。(要求:鍵盤輸入通過流封裝獲取,不要使用Scanner 類)package class InputStreamTest public static void main(String args) BufferedReader in = new BufferedR

17、eader(new InputStreamReader);String line=null;try" 請輸入第一個正整數(shù)a: ");line = ();int a = (line);" 請輸入第二個正整數(shù)b: ");line = ();int b = (line);"a+b=" + (a+b);catch(Exception e)" 輸入錯誤!");(0);1)輸入3個學生的姓6.設計學生類Student ,屬性:編號(整型);姓名(字符串),成績(整型)。編寫一個程序:要求:(名和成績,將其姓名和成績保存到中;(

18、2)然后從該文件中讀取數(shù)據(jù),求得這五個學生的平均成績package class Student int id;String name;int score;Student()public void setId(int id)=id;public void setName(String name)=name;public void setScore(int score)=score;public String toString()return + "t" + + "t" + + ”n"package class Main public static void main(String口 args) BufferedReader in = new BufferedReader(new InputStreamReader);tryDataOutputStream out = new DataOutputStream(new FileOutpu

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論