解析Java程序设计答案.docx_第1页
解析Java程序设计答案.docx_第2页
解析Java程序设计答案.docx_第3页
解析Java程序设计答案.docx_第4页
解析Java程序设计答案.docx_第5页
已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论