当前位置:文档之家› JAVA第五次实验报告

JAVA第五次实验报告

实验一:编写一个Java Application程序,打印命令行输入的所有参数。

实验源代码:package .bing;import java.util.Scanner;public class Scanner_01 {public static void main(String[] args) { String name;String Sage;int age;String Ssore;double sore;Scanner in = new Scanner(System.in);System.out.println("请输入你的姓名:");name = in.nextLine();System.out.println("请输入您的年龄:");Sage = in.nextLine();age = Integer.parseInt(Sage);System.out.println("请输入您的分数");Ssore =in.nextLine();sore = Double.parseDouble(Ssore);System.out.println(name+" "+age+" "+sore);}}运行结果:实验题2 阅读下面程序,叙述其功能程序功能:从指定的文件中读出数据。

实验题 3 设计一个类FileRWTest,实现从input.txt文件中读入数据到字符数组cBuffer中,然后再写入到文件“output.txt”中。

[基本要求] 编写完整程序。

程序源代码:package .bing;import java.io.*;public class FileRWTest {public static void main(String[] args) throws FileNotFoundException{int rs;File file = new File("D:\\","input.txt");File fileout = new File("D:\\","Output.txt");FileInputStream fis = new FileInputStream(file);FileOutputStream fos = newFileOutputStream(fileout,true);InputStreamReader isr = new InputStreamReader(fis);OutputStreamWriter out = new OutputStreamWriter(fos);try {while((rs = isr.read())!=-1){char cBuffer=(char)rs;out.write((int)cBuffer);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {isr.close();out.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}实验题 4"水仙花数"。

所谓"水仙花数"是指一个3位数,其各位数字立方和等于该数本身。

例如,153就是一水仙花数,因为153 = 13+53+33。

请计算出100万以内所有的水仙花数,并以一行一个水仙花数的格式保存到文本文件data.txt中。

输出格式如下:153 = 1*1*1 + 5*5*5 + 3*3*3程序源代码:package .bing;import java.io.*;public class Daffodil {public static void Check(){Integer num;for(num=100;num<1000;num++){String str = new String(num.toString());char []s = new char[str.length()];s = str.toCharArray();int sum=0;for(int i=0;i<str.length();i++){int m = (s[i]-'0');int n = m*m*m;sum+=n;}if(num == sum ){StringBuffer aline = new StringBuffer();aline.append(num+" = ");for(int i=0;i<3;i++){if(i!=2)aline.append(s[i]+"*"+s[i]+"*"+s[i]+"*"+" + ");elsealine.append(s[i]+"*"+s[i]+"*"+s[i]);}String al = new String(aline);File file = new File("F:\\shuixianhua.txt");FileWriter fos;try {fos = new FileWriter(file);BufferedWriter bw = new BufferedWriter(fos);bw.write(al+"\n");bw.newLine();bw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(al);}}}public static void main(String[] args) {// TODO Auto-generated method stubCheck();}}运行结果:实验题 5写一程序统计纯文本文件“Early-Precaution.txt”的大写字母、小写字母个数,并将所有小写字母转换为大写字母,输出到result.txt程序源代码:package .bing;import java.io.*;public class Getchar {FileReader fr;public static void main(String[] args) {FileReader fr;try {fr = new FileReader("F:\\Early-Precaution.txt");File file = new File("F:\\result.txt");FileWriter fos =new FileWriter(file);BufferedReader br = new BufferedReader(fr);BufferedWriter bw = new BufferedWriter(fos);String aline;int k=0;int m=0;while((aline = br.readLine())!=null){String str = new String (aline);char []s = new char[str.length()];s=str.toCharArray();for(int i=0;i<str.length();i++){if(s[i]>='a'&& s[i]<='z'){k++;}else if(s[i]>='A' && s[i]<='Z'){m++;}}String STR = str.toUpperCase();bw.write(STR + "\n");}br.close();bw.close();System.out.println("小写字母的个数为:");System.out.println(k);System.out.println("大写字母的个数为:");System.out.println(m);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}程序运行结果:实验总结:1、对String类有了进一步的认识。

2、了解并掌握了基本的输入输出流的实现方法。

3、字符流和字节流有了更为深刻的认识。

相关主题