




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、1. 編寫程序,要求:用戶在鍵盤每輸入一行文本,程序將這段文本顯示在控制臺中。當用戶輸入的一行文本是“exit ” (不區分大小寫)時,程序將用戶所有輸入的文本都寫入到文件中,并退出。(要求:控制臺輸入通過流封裝獲取,不要使用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 ,屬性:學號(整型);姓名(字符串),選修課程(名稱)及課程成績(整型)。編寫一個控制臺程序,能夠實現Student 信息的保存、讀取。具體要求:(1)提供 Student 信息的保存功能:通過控
7、制臺輸入若干個學生的學號、姓名以及每個學生所修課程的課程名和成績,將其信息保存到中;(2)數據讀取顯示:能夠從文件中讀取學生及其課程成績并顯示于控制臺。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 結束信息錄入, 輸入其他繼續錄入");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" 已有學生人數:"+();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. 編寫程序,在控制臺窗口提示輸入兩個整數,然后接收這兩個整數,并輸出它們的和。(要求:鍵盤輸入通過流封裝獲取,不要使用Scanner 類)package class InputStreamTest public static void main(String args) BufferedReader in = new BufferedR
17、eader(new InputStreamReader);String line=null;try" 請輸入第一個正整數a: ");line = ();int a = (line);" 請輸入第二個正整數b: ");line = ();int b = (line);"a+b=" + (a+b);catch(Exception e)" 輸入錯誤!");(0);1)輸入3個學生的姓6.設計學生類Student ,屬性:編號(整型);姓名(字符串),成績(整型)。編寫一個程序:要求:(名和成績,將其姓名和成績保存到中;(
18、2)然后從該文件中讀取數據,求得這五個學生的平均成績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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 民間個人借款合同(集合15篇)
- 攀枝花輔警考試題庫2025(有答案)
- 能源行業智能監控系統設計與實現方案
- 慢性心力衰竭基層診療指南(2024年)
- 海洋氫能產業發展潛力
- 2025年內蒙古中煤集團鄂爾多斯山不拉煤礦校園招聘考試筆試試題(含答案)
- 海洋文化與經濟融合發展路徑
- 老師的音樂課件
- 2025年氨基酸運動飲料食品市場分析報告
- 菜園土地流轉與農業產業化合作合同
- LINE6效果器HD300中文說明書
- 可編程控制技術課件
- 2025年目標管理實施與評估考試試卷及答案
- 【邵陽】2024年湖南邵陽新邵縣事業單位招聘工作人員44人筆試附帶答案詳解
- 外賣餐飲平臺管理制度
- 2025年青浦區區管企業統一招聘55人筆試參考題庫附帶答案詳解
- 2025年河南省鐵路建設投資集團有限公司招聘筆試參考題庫附帶答案詳解
- 2025年中考語文作文終極押題(附范文5篇)
- 一網一平臺電大《西方經濟學(經濟學)》形考任務1-6終結性考試題庫答案
- 股權質押合同違約處理協議
- 初中德育教育家長會
評論
0/150
提交評論