实验1 分析成绩单
一、实验目的:掌握字符输入、输出流用法。
二、实验代码:
Fenxi:
import java.util.*;
public class Fenxi{
public static double getTotalScore(String s){
Scanner scanner=new Scanner(s);
scanner.uDelimiter("[^0123456789.]+");
double totalScore=0;
while(scanner.hasNext()){
try{
double Double();
totalScore=totalScore+score;
}
catch(InputMismatchException exp){
String ();
}
}
return totalScore;
}
}
AnalysisResult:
import java.io.*;
import java.util.*;
public class AnalysisResult{
public static void main(String args[]){
File fRead=new File("");
File fWrite=new File("");
try{
Writer out= new FileWriter(fWrite,true);//以尾加方式创建指向文件fWrite的out流
BufferedWriter bufferWrite=new BufferedWriter(out); //创建指向out的bufferWrite流
Reader in=new FileReader(fRead); //创建指向文件fRead的in流
BufferedReader bufferRead=new BufferedReader(in); //创建指向in的bufferRead流
String str=null;
节电小妙招 while ((adLine())!=null){
double totalScore =TotalScore(str);
str=str+"总分:"+totalScore;
System.out.println(str);
bufferWrite.write(str);
wLine();
}
bufferRead.clo();
bufferWrite.clo();
}
catch(IOException e){
System.out.String());
}
}厦门导游
}
三、结果截图:
四、实验分析:
(1)改进程序,使得能统计出每个学生的平均成绩。
答:
(2)现在有如下格式的货物明细(文本格式)
品名:电视,length:102 cm,width:89 cm,height:56 cm.
品名:轿车,length:4502 cm,width:178 cm,height:156 cm.
品名:桌子,length:125 cm,width:78 cm,height:68 cm.
答:
CalculateVolume.java
import java.io.*;
import java.util.*;
public class CalculateVolume{
public static void main(String args[]){
File fRead=new File("");
File fWrite=new File("");
try{Writer out=new FileWriter(fWrite,true);
BufferedWriter bufferWrite=new BufferedWriter(out);
Reader in=new FileReader(fRead);
BufferedReader bufferRead=new BufferedReader(in);
String str=null;
while((adLine())!=null){艾青诗选好词好句
double Volume(str);
str=str+"体积:"+s+"cm^3";
System.out.println(str);
bufferWrite.write(str);
wLine();
}
bufferRead.clo();
bufferWrite.clo();
}
catch(IOException e){
System.out.String());
}
苏州乐园 }
}
Jisuan.java
import java.util.*;
public class Jisuan{
public static double getVolume(String s){
Scanner scanner=new Scanner(s);
scanner.uDelimiter("[^0123456789.]+");
double volume=1;
while(scanner.hasNext()){
try{
描写竹子的句子 double Double();
volume=volume*distance;
}
catch(InputMismatchException exp){
String ();
掉睫毛
}
}
return volume;
}
班队会
}
实验2 统计英文单词
一、实验目的:掌握使用Scanner类解析文件。
二、实验代码:
Dictionary.java:
import java.util.*;
public class Dictionary{
public static void main(String args[]){
Vector<String> allWord,noSameWord;
WordStatistic statistic=new WordStatistic();
statistic.tFileName("");
statistic.wordStatistic();//statistic调用WordStatistic()方法
AllWord();高跟鞋排名
NoSameWord();
System.out.println("共有"+allWord.size()+"个英文单词");
System.out.println("有"+noSameWord.size()+"个互不相同英文单词");
System.out.println("按字典顺序排列:");
String s[]=new String [noSameWord.size()];
for(int i=0;i<noSameWord.size();i++){
s[i]=noSameWord.elementAt(i);
}
Arrays.sort(s);
for(int i=0;i<noSameWord.size();i++){
System.out.println(s[i]+" ");
}
}
}
OutputWordMess.java:
import java.util.*;
public class OutputWordMess{
public static void main(String args[]){
Vector<String> allWord,noSameWord;
WordStatistic statistic=new WordStatistic();
statistic.tFileName("");
statistic.wordStatistic();//statistic调用WordStatistic()方法
AllWord();
NoSameWord();
System.out.println("共有"+allWord.size()+"个英文单词");
System.out.println("有"+noSameWord.size()+"个互不相同英文单词");
System.out.println("按出现频率排列:");
int count[]=new int[noSameWord.size()];
for(int i=0;i<noSameWord.size();i++){
String s1=noSameWord.elementAt(i);
for(int j=0;j<allWord.size();j++){
String s2=allWord.elementAt(j);
if(s1.equals(s2))
count[i]++;
}
}
for(int m=0;m<noSameWord.size();m++){
for(int n=m+1;n<noSameWord.size();n++){
if(count[n]>count[m]){
String temp=noSameWord.elementAt(m);
noSameWord.tElementAt(noSameWord.elementAt(n),m);
noSameWord.tElementAt(temp,n);
int t=count[m];
count[m]=count[n];
count[n]=t;
}
}
}
for(int m=0;m<noSameWord.size();m++){
double frequency=(1.0*count[m])/allWord.size();
System.out.printf("%s:%-7.3f",noSameWord.elementAt(m),frequency);