我是菜鸟,高手们的话我理解不上去,所以到现在还未解决,现在把我的原题和自己写的程序发上去,如果高手们帮我解决了,我愿意把我所有的分都送你们. 问题是:从键盘获取名字,语文,英语,数学,科学的成绩,然后填充到Students对象中,并将对象存如入文件。代码如下: package students1; import java.io.*; class Students implements Serializable{ String name; int[] record=new int[4]; int total; float avg; String grade; public Students(){} public String getName(){return name;} public int[] getRecord(){return record;} public int getTotal(){ int s=0; for(int i=0;i<record.length;i++) s+=record[i]; return s; } public float getAvg(){ float f=getTotal()/4f; return f; } public String getGrade(){ String str; if(avg<100&&avg>90) str="A"; else if(avg>80) str="B"; else if(avg>70) str="C"; else if(avg>60) str="D"; else str="F"; return str; } } public class Students1 { public static int s=0; /** Creates a new instance of Students1 */ public Students1() { }
找不到包里的类可能是因为未编译,选中make一下
不是那个原因啊.编译后提示这个包不存在啊,可是确实有students1这个包啊,我都检查好几遍了.真郁闷啊.
2 如果你用CLASSPATH,难道你用命令行?
3 如果是,你可以把你的class打包成jar,然后放到CLASSPATH里面
然后接你的class~~
或者可以试试import students1.[你需要的class名称];
有时候是会有那样的问题
如果你是命令行编译的,一定要确保包所在的文件目录结构正确:
例如:
project(文件夹)
-- students1(文件夹名称,注意要和你的包名一致)
-- students1包中的所有XXX.java,以及这些java文件的XXX.class
-- XXXX文件夹(你引入students1的那个java文件所在的包)
-- XXXX.java,XXXX.class
楼上所说的方法,也就是把这个包添加到project的build libriary ,这个方法是一定有效的,只要你的程序没问题
那么先设置你的classpath D:\mypackage\students1;
然后再导入 import mypackage.students1.*;
2 在你的CLASSPATH 里面,加入这个jar
3 如果是eclipse 则在Build Path 里面增加自定义的类库
问题是:从键盘获取名字,语文,英语,数学,科学的成绩,然后填充到Students对象中,并将对象存如入文件。代码如下:
package students1;
import java.io.*;
class Students implements Serializable{
String name;
int[] record=new int[4];
int total;
float avg;
String grade;
public Students(){}
public String getName(){return name;}
public int[] getRecord(){return record;}
public int getTotal(){
int s=0;
for(int i=0;i<record.length;i++)
s+=record[i];
return s;
}
public float getAvg(){
float f=getTotal()/4f;
return f;
}
public String getGrade(){
String str;
if(avg<100&&avg>90) str="A";
else if(avg>80) str="B";
else if(avg>70) str="C";
else if(avg>60) str="D";
else str="F";
return str;
}
}
public class Students1 {
public static int s=0;
/** Creates a new instance of Students1 */
public Students1() {
}
public static void main(String[] args) throws Exception{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("请顺序输入名字及国语,英语,数学,科学的成绩.(eof:输入完毕)");
String data;
String[] str=new String[5];
FileOutputStream fos=new FileOutputStream("D:\\xi.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
while (true){
data=br.readLine();
s++;
if(data.equals("eof")) break;
str=data.split(" ");
Students stu=new Students();
stu.name =str[0];
stu.record[0]=Integer.parseInt(str[1]);
stu.record[1]=Integer.parseInt(str[2]);
stu.record[2]=Integer.parseInt(str[3]);
stu.record[3]=Integer.parseInt(str[4]);
stu.total=stu.getTotal();
stu.avg=stu.getAvg();
stu.grade=stu.getGrade();
oos.writeObject(stu);
}
oos.close();
System.out.println("文件内容");
FileInputStream fis=new FileInputStream("D:\\xi.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
for(int j=1;j<s;j++){
Students stud;
stud=(Students)ois.readObject();
System.out.println("Students对象"+j+" "+"{"+stud.name+" "+stud.record[0]+" "+stud.record[1]+" "+stud.record[2]+" "+stud.record[3]+" "+stud.total+" "+stud.avg+" "+stud.grade+"}");
}
ois.close();
}
}
这个程序没问题,不过下一个程序就出错了,因为要用第一个程序中的类
2 从上题创建的文件中读取对象,并按平均分降序排列。代码如下:
package students2;
import java.io.*;
import students1.*;
public class Students2 {
public Students2() {
}
public static void main(String[] args) throws Exception{
// TODO code application logic here
int d=Students1.s;
int[] a=new int[d];
FileInputStream fis=new FileInputStream("D:\\xi.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Students[] students=new Students[d];
for(int i=1;i<d;i++){
students[i]=(Students)ois.readObject();
}
ois.close();
System.out.println("名字 "+"国语 "+"英语 "+"数学 "+"科学 "+"总分 "+"平均分 "+"学分 "+"顺序");
for(int i=1;i<d;i++) {
int max=i;
for( int j=i;j<d;j++){
if(students[j].avg>students.avg) max=j;
}
System.out.println(students[max].name+" "+students[max].record[0]+" "+students
[max].record[1]+ " "+students[max].record[2]+" "+students[max].record[3]+" "+students
[max].total+" "+students[max].avg+" "+students[max].grade+" "+i);
}
}
}
错误总是提示students1包不存在,如果可以的话您运行一下吧,我在netbeans里运行的,帮我找一下错误的原因啊...
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;class Students implements Serializable {
String name; int[] record = new int[4]; int total; float avg; String grade; public Students() {
} public String getName() {
return name;
} public int[] getRecord() {
return record;
} public int getTotal() {
int s = 0;
for (int i = 0; i < record.length; i++)
s += record[i];
return s;
} public float getAvg() {
float f = getTotal() / 4f;
return f;
} public String getGrade() {
String str;
if (avg < 100 && avg > 90)
str = "A";
else if (avg > 80)
str = "B";
else if (avg > 70)
str = "C";
else if (avg > 60)
str = "D";
else
str = "F";
return str;
}
}public class Students1 {
public static int s = 0; /** Creates a new instance of Students1 */
public Students1() {
} public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("请顺序输入名字及国语,英语,数学,科学的成绩.(eof:输入完毕)");
String data;
String[] str = new String[5];
FileOutputStream fos = new FileOutputStream("D:\\xi.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
while (true) {
data = br.readLine();
s++;
if (data.equals("eof"))
break;
str = data.split(" ");
Students stu = new Students();
stu.name = str[0];
stu.record[0] = Integer.parseInt(str[1]);
stu.record[1] = Integer.parseInt(str[2]);
stu.record[2] = Integer.parseInt(str[3]);
stu.record[3] = Integer.parseInt(str[4]);
stu.total = stu.getTotal();
stu.avg = stu.getAvg();
stu.grade = stu.getGrade();
oos.writeObject(stu);
}
oos.close();
System.out.println("文件内容");
FileInputStream fis = new FileInputStream("D:\\xi.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
for (int j = 1; j < s; j++) {
Students stud;
stud = (Students) ois.readObject();
System.out.println("Students对象" + j + " " + "{" + stud.name + " " + stud.record[0] + " " + stud.record[1] + " "
+ stud.record[2] + " " + stud.record[3] + " " + stud.total + " " + stud.avg + " " + stud.grade + "}");
}
ois.close();
}
}class Students2 {
public Students2() {
} public static void main(String[] args) throws Exception {
// TODO code application logic here
int d = Students1.s;
int[] a = new int[d];
FileInputStream fis = new FileInputStream("D:\\xi.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Students[] students = new Students[d];
for (int i = 1; i < d; i++) {
students[i] = (Students) ois.readObject();
}
ois.close();
System.out.println("名字 " + "国语 " + "英语 " + "数学 " + "科学 " + "总分 " + "平均分 " + "学分 " + "顺序");
for (int i = 1; i < d; i++) {
int max = i;
for (int j = i; j < d; j++) {
if (students[j].avg > students[i].avg)
max = j;
}
System.out.println(students[max].name + " " + students[max].record[0] + " " + students[max].record[1] + " "
+ students[max].record[2] + " " + students[max].record[3] + " " + students[max].total + " " + students[max].avg
+ " " + students[max].grade + " " + i);
}
}
}
netbean页应该有这功能....................
我想多说两句:你还没有包的概念,,,,,哪里有像你这么打包的?
看看基础吧,不要急着看运行结果........