版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小学研学活动方案6篇
- 工程造价咨询服务合同范本9篇
- 学校矛盾纠纷排查工作情况汇报三篇
- 中国小动物技能大赛骨科专赛理论考试题库(含答案)
- 《反电信网络诈骗法》知识考试题库150题(含答案)
- 大拇指腱鞘炎偏方课件
- 2025年河北女子职业技术学院高职单招语文2018-2024历年参考题库频考点含答案解析
- 2025年江西现代职业技术学院高职单招高职单招英语2016-2024历年频考点试题含答案解析
- 2025年江西冶金职业技术学院高职单招语文2018-2024历年参考题库频考点含答案解析
- 2025年武汉职业技术学院高职单招职业技能测试近5年常考版参考题库含答案解析
- 2025年度新能源汽车充电站运营权转让合同样本4篇
- 第5课 隋唐时期的民族交往与交融 课件(23张) 2024-2025学年统编版七年级历史下册
- 2024年全国职业院校技能大赛高职组(生产事故应急救援赛项)考试题库(含答案)
- 老年上消化道出血急诊诊疗专家共识2024
- 广东省广州黄埔区2023-2024学年八年级上学期期末物理试卷(含答案)
- 学校安全工作计划及行事历
- 《GMP基础知识培训》课件
- 数学家华罗庚课件
- 贵州茅台酒股份有限公司招聘笔试题库2024
- 《纳米技术简介》课件
- 血液透析高钾血症的护理查房
评论
0/150
提交评论