导航:首页 > 编程语言 > javaio流练习题

javaio流练习题

发布时间:2023-05-05 21:18:44

java iO 流问题

这个可以改成:Scanner sc=new Scanner(System.in);

经过包装后,程序是一行一行读,当读到"end"程序就终止了。br.readLine(),一次读一行。

还有while(len!=-1)的,当读字节时,就不可以用这样的方法了

Ⅱ java题目,io流问题

我这里有一个简单的学生管理系统,你只需要把Student学生类修改成名片类就可以了。你需要新建立一个java文件名为HW4.java,复制粘贴以下代码,编译运行就可以了。

importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.io.Serializable;
importjava.util.Arrays;
importjava.util.Comparator;
importjava.util.Scanner;

<Student>,Serializable{
/**
*SerializableUID:ensuresserialize/de-.
*/
=-3515442620523776933L;

publicintgetNumber(){
returnnumber;
}

publicvoidsetNumber(intnumber){
this.number=number;
}

publicintgetAge(){
returnage;
}

publicvoidsetAge(intage){
this.age=age;
}

publicdoublegetWeight(){
returnweight;
}

publicvoidsetWeight(doubleweight){
this.weight=weight;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

privateintnumber;
privateintage;
privatedoubleweight;
privateStringname;

publicStudent(intnumber,intage,doubleweight,Stringname){
this.number=number;
this.age=age;
this.weight=weight;
this.name=name;
}

@Override
publicintcompareTo(Studento){
if(this.age==o.age){
return(int)(this.weight-o.weight);
}
returnthis.age-o.age;
}
}

<Student>{

@Override
publicintcompare(Studento1,Studento2){
returno1.getAge()-o2.getAge();
}
}

<Student>{

@Override
publicintcompare(Studento1,Studento2){
return(int)(o1.getWeight()-o2.getWeight());
}
}

publicclassHW4{
//.
publicstaticvoidmain(String[]args){
System.out.println(" WelcometotheSystem,Chooseoptionsbelow:");
printPrompt();

Student[]students=null;
Scannerscanner=newScanner(System.in);
while(scanner.hasNextInt()){
switch(scanner.nextInt()){
case1:
System.out.println("PrintStudentInformation");
if(students==null){
System.out.println("PleaseInitiliseNstudentsfirst");
}else{
printStudents(students);
}
printPrompt();
break;
case2:
System.out.println(":");
intnumber=scanner.nextInt();
students=initilise(number,scanner);
printPrompt();
break;
case3:
System.out.println("Addanewstudent");
printPrompt();
if(students==null){
System.out.println("PleaseInitiliseNstudentsfirst");
}else{
intnewLength=students.length+1;
students=Arrays.Of(students,newLength);
students[newLength-1]=createStudent(scanner);
System.out.println("Newstudenthasbeenadded.");
printPrompt();
}
break;
case4:
System.out.println("SortingbyAge,WeightinAsceorder");
if(students==null){
System.out.println("PleaseInitiliseNstudentsfirst");
}else{
Student[]sortedStudents=students;
Arrays.sort(sortedStudents);
printStudents(sortedStudents);
}
break;
case5:
System.out.println("CalcaulteMin,Max,AveofAgeandWeight");
if(students==null){
System.out.println("PleaseInitiliseNstudentsfirst");
}else{
Student[]sortedAgeStudents=students;
Arrays.sort(sortedAgeStudents,newStudentSortByAge());
Student[]sortedWeightStudents=students;
Arrays.sort(sortedWeightStudents,newStudentSortByWeight());
intaverageAge=0;
doubleaverageWeight=0.0;
for(Studentstudent:sortedAgeStudents){
averageAge+=student.getAge();
averageWeight+=student.getWeight();
}
averageAge=averageAge/sortedAgeStudents.length;
averageWeight=averageWeight/sortedWeightStudents.length;
System.out.printf("MinAge:%d,MaxAge:%d,AveAge:%d ",sortedAgeStudents[0].getAge(),sortedAgeStudents[sortedAgeStudents.length-1].getAge(),averageAge);
System.out.printf("MinWeight:%f,MaxWeight:%f,AveWeight:%f ",sortedWeightStudents[0].getWeight(),sortedWeightStudents[sortedWeightStudents.length-1].getWeight(),averageWeight);
}
break;
case6:
System.out.println("WriteStudentdataintofile");
try(ObjectOutputStreamoos=newObjectOutputStream(newFileOutputStream(newFile("studentsData"),true))){
oos.writeObject(students);
printPrompt();
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
break;
case7:
System.out.println("ReadstudentDatafromfile");
try(ObjectInputStreamois=newObjectInputStream(newFileInputStream(newFile("studentsData")))){
students=(Student[])(ois.readObject());
printPrompt();
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
break;
default:
scanner.close();
System.out.println("Quit");
break;
}
}
}

publicstaticvoidprintPrompt(){
System.out.println("1:Displaycurrentstudents");
System.out.println("2:InitiliseNstudents");
System.out.println("3:Addnewstudent");
System.out.println("4:SortingbyAge,WeightinAsceorder");
System.out.println("5:CalcaulteMin,Max,AveofAgeandWeight");
System.out.println("6:WriteStudentdataintofile");
System.out.println("7:ReadstudentDatafromfile");
}

publicstaticStudent[]initilise(intn,Scannerscanner){
Student[]students=newStudent[n];
for(inti=0;i<n;i++){
students[i]=createStudent(scanner);
}
System.out.println(".");
returnstudents;
}

publicstaticvoidprintStudents(Student[]students){
for(Studentstudent:students){
System.out.printf("Student:%s,Number:%d,Age:%d,Weight:%f ",student.getName(),student.getNumber(),student.getAge(),student.getWeight());
}
}

(Student[]students){
for(Studentstudent:students){
System.out.printf("Age:%d,Weight:%f,Student:%s,Number:%d ",student.getAge(),student.getWeight(),student.getName(),student.getNumber());
}
}

(Scannerscanner){
intstudentNumber=0,studentAge=0;
doublestudentWeight=0.0;
StringstudentName=null;
System.out.println("EnterStudentNumber:");
while(scanner.hasNext()){
studentNumber=scanner.nextInt();

System.out.println("EnterStudentAge:");
studentAge=scanner.nextInt();

System.out.println("EnterStudentWeight:");
//nextDouble仅仅读取double的值,在double值后的' '将会被nextLine()所读取,所以读取studentName时需要再读取一次nextLine()
studentWeight=scanner.nextDouble();

System.out.println("EnterStudentName:");
scanner.nextLine();
studentName=scanner.nextLine();
break;
}
returnnewStudent(studentNumber,studentAge,studentWeight,studentName);
}
}
阅读全文

与javaio流练习题相关的资料

热点内容
iphone5如何升级4g网络 浏览:5
团购是在哪个app 浏览:897
打开多个word文档图片就不能显示 浏览:855
腾讯新闻怎么切换版本 浏览:269
app安装失败用不了 浏览:326
桌面文件鼠标点开会变大变小 浏览:536
手机误删系统文件开不了机 浏览:883
微信兔子甩耳朵 浏览:998
android蓝牙传文件在哪里 浏览:354
苹果6s软解是真的吗 浏览:310
c语言代码量大 浏览:874
最新网络卫星导航如何使用 浏览:425
以下哪些文件属于图像文件 浏览:774
zycommentjs 浏览:414
确认全血细胞减少看哪些数据 浏览:265
文件有哪些要求 浏览:484
cad打开时会出现两个文件 浏览:65
什么是转基因网站 浏览:48
手柄设备有问题代码43 浏览:921
怎么他么怎么又网络了 浏览:649

友情链接