




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java作业P201:5如何改进下面的程序以提高其执行性能?对你的改进作出解释,并写出新的程序。int i;URL url = new URL(/);URLConnection javaSite = url.openConnection();InputStream input = javaSite.getInputStream();InputStreamReader reader = new InputStreamReader(input);while (i = reader.read() != -1) System.out.print(i);答: 使用缓冲流!在这里,可以增加两个缓冲流: 在InputStream 上增加一个BufferedInputStream ,在InputStreamReader 上增加一个BufferedReader。改变后的程序如下:int i;URL url = new URL(/);URLConnection javaSite = url.openConnection();InputStream input = javaSite.getInputStream();BufferedInputStream in = new BufferedInputStream(input);BufferedReader reader = new BufferedReader(new InputStreamReader(in);while (i = reader.read() != -1) System.out.print(i);6查阅API文档中有关DataInputStream和DataOutputStream的内容。并编写一个程序使用readInt()方法从输入文件中读入学生成绩,求出学生的总成绩和平均成绩输出到另一个文件中。假设输入文件中的内容格式如下:姓名 语文 数学 外语张三 89 92 95李四 77 81 74王五 87 80 75答:public static String readInputByRow(String path,int num) File file=new File(path); try FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, UTF-8); BufferedReader reader = new BufferedReader(isr); String tempstring=; int line=1; while(tempstring=reader.readLine()!=null) if(line=num) break; line+; reader.close(); isr.close(); fis.close(); return tempstring; catch (IOException e) e.printStackTrace(); return null; 10.改正下面的程序使其能通过编译答:public class catvoid test(File named)RandomAccessFile BufferedReader input = null;String line = null;tryinput=newRandomAccessFile(named,r);while(line = input.readLine()!=null)System.out.println(line);return;catch(FileNotFoundExceptionfnf)System.err.println(File:+named+notfound.);catch(Exceptione)System.err.println(e.toString();)finallyif(input!=null)tryinput.close();catch(IOExceptionio)p173:5:下面的程序片段有什么错误?能否通过编译?trycatch(Exception e)catch(ArithemeticException a)答:不能通过编译,ArithemeticException cannot be resolved to a typeDuplicate parameter a6:阅读程序,写出程序的运行结果。class first_exception public static void main(String args) char c; int a,b=0; int array=new int7; String s=Hello; try a=1/b; catch(ArithmeticException ae) System.out.println(Catch +ae); try array8=0; catch(ArrayIndexOutOfBoundsException ai) System.out.println(Catch +ai); try c=s.charAt(8); catch(StringIndexOutOfBoundsException se) System.out.println(Catch +se); 答:运行结果:Catch java.lang.ArithmeticException: / by zeroCatch java.lang.ArrayIndexOutOfBoundsException: 8Catch java.lang.StringIndexOutOfBoundsException: String index out of range: 89: 试编写一个程序读入英文的Zero到Nine,输出其对应的阿拉伯数字,要求能对错误的输入按异常情况进行处理。程序:public String nvl(String str,String newStr) return str = null | .equalsIgnoreCase(str)?newStr:str; public void output(String numberStr) numberStr = nvl(numberStr,wrongNumber); if(one.equalsIgnoreCase(numberStr) System.out.println(1); return; if(two.equalsIgnoreCase(numberStr) System.out.println(2); return; if(three.equalsIgnoreCase(numberStr) System.out.println(3); return; if(four.equalsIgnoreCase(numberStr) System.out.println(4); return; if(five.equalsIgnoreCase(numberStr) System.out.println(5); return; if(six.equalsIgnoreCase(numberStr) System.out.println(6); return; if(seven.equalsIgnoreCase(numberStr) System.out.println(7); return; if(eight.equalsIgnoreCase(numberStr) System.out.println(8); return; if(nine.equalsIgnoreCase(numberStr) System.out.println(9); return; try if(numberStr.equalsIgnoreCase(wrongNumber) int a = 1 / 0; catch(Exception e) System.out.println(错误的输入!); 12:import java.io.*;import java.util.Scanner;public class num public static void main(String args) / TODO Auto-generated method stubchar data=new char12;int i,b;String number;Scanner sc=new Scanner(System.in);trynumber=sc.nextLine();data=number.toCharArray();catch(Exception e)System.out.print(e.toString();tryif(data0!=0)b=1/0;catch (ArithmeticException e) System.out.print(区号必须0开头); tryfor(i=0;i12;i+)if(i!=3&i!=4)if(!(0=datai&datai=9)b=1/0;catch (ArithmeticException e) System.out.print(其他数字不为数字); tryif(data3=-)if(data4=0)b=1/0;else if(data4=-)if(data5=0)b=1/0;catch (ArithmeticException e) System.out.print(号
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《百分数的应用(二)》(教学设计)-2024-2025学年北师大版小学数学六年级上册
- 2024年山西省忻州市五寨县中考一模语文试题(原卷版+解析版)
- 绿色生活未来可期
- 立春节气与儿童成长
- 楼盘销售提效之道
- 故事代替道理:《有趣的数学》
- 2025年湖南省农药购销合同模板
- 2025年租房合同协议书范本
- 2025合同解除协议格式范本
- 2025无需抵押个人消费贷款合同模板
- 2024年广西安全员B证试题题库
- T-GDCKCJH 091-2024 微生物电化学法水质生物毒性现场快速检测技术规范
- 【MOOC】健身气功八段锦-东南大学 中国大学慕课MOOC答案
- 周杰伦原版现场《默》钢琴弹唱钢琴谱钢琴简谱数字谱钢琴
- 珍奇观赏植物知到智慧树章节测试课后答案2024年秋西南大学
- 2024湖北中烟工业限责任公司2024年招聘(博士岗位)【5人】管理单位遴选500模拟题附带答案详解
- 《特种设备重大事故隐患判定标准》培训
- 糖尿病足创面护理
- 省际联盟骨科创伤类医用耗材集中带量采购中选结果
- 专题讲座3-射电望远镜综述
- 2024年重庆市中考化学试题(A卷)含答案
评论
0/150
提交评论