JAVA高手帮忙!这个程序哪里出错。

来自:    更新日期:早些时候
请JAVA高手帮我看一下这个程序哪里有错???~

你的程序里的语句滴E[] a = new [10],少了E类型,没有[10]这种类型肯定出错。。。就跟你说的,应该是E[] a = new E[10],但是对于泛型,最好还是要明确泛型的类型(否则1.5版本以上的JDK就会给出警告),即new E[10],否则默认泛型的类型就是Object类型。

for(i=0;i<4;j++)//这里应该是i++的吧?
{
for(j=0;j<5;i++)//这里应该是j++的吧?
{
seasonSum[i]+=sales[j][i];
}
System.out.println("所有部门"+"季度"+(i+1)+"的总和是:"+seasonSum[i]);
}

改成:
for(i=0;i<4;i++)
{

for(j=0;j<5;j++)
{
seasonSum[i]+=sales[j][i];
}
System.out.println("所有部门"+"季度"+(i+1)+"的总和是:"+seasonSum[i]);
}

首先,不能说是你的程序错,只是有很多的不合理,不合乎标准罢了,运行没有什么问题,只是有点不伦不类罢了,我说几点吧:
1、类成员属性和成员字段是有区别的,你的题目中说的是定义属性,但是你却在你的Student类中中定义了三个字段,而且是public的,就不合理,javaBean规定,属性是以get/is(读)、set(写)开始的“方法”而不是公共的字段,具体细节很多,希望你能够去多看看文档!
2、在你给e1.score = (float)111.4;赋值的时候,我不知道你为什么使用(float)进行强制类型转换,显的有点多余,但是如果你真的想显示它是个float的,你大可以写成:e1.score = 111.4f;
3、下面的这段代码我就有点看不懂了:
dis = new DataInputStream(new FileInputStream("/home/huihui/Desktop/student.doc"));
System.out.println(dis.readString());
System.out.println(dis.readInt());
System.out.println(dis.readFloat());
自然是对象的序列化,但是为什么会是这样读取呢?你是为了测试一下什么吗?其实你可以用文本编译器打开那个student.doc文件看一下的
4、最后弱弱的提一句,序列化对象,最好是能够提供一个序列化ID;
5、补充、人家说的是要带参数的构造函数,但是你没有!

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
*
* @author top
*/
public class Inout {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
Student student1 = new Student("top_beidu", 23, 99.9f);
Student student2 = new Student("student2", 24, 69.9f);
Student student3 = new Student("student3", 25, 89.9f);
System.out.println("输入的是:");
student1.print();
student2.print();
student3.print();
FileOutputStream fos = new FileOutputStream("student.txt");
// FileOutputStream fos = new FileOutputStream("F:/student.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(student1);
oos.writeObject(student2);
oos.writeObject(student3);
oos.close();
FileInputStream fis = new FileInputStream("student.txt");
// FileInputStream fis = new FileInputStream("F:/student.txt");

ObjectInputStream ois = new ObjectInputStream(fis);
Student stu1 = (Student) ois.readObject();
Student stu2 = (Student) ois.readObject();
Student stu3 = (Student) ois.readObject();
System.out.println("输出的是:");
stu1.print();
stu2.print();
stu3.print();
ois.close();
}
}

class Student implements Serializable {

private String name;
private int age;
private float score;

public Student(String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
}

public void print() {
System.out.println("name=" + name + ";age=" + age + ";score=" + score);
}
}

package practice.donghua.org;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;

/**
*
* @author Administrator
*/
class Student implements Serializable
{
private String name;
private float score;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public float getScore() {
return score;
}

public void setScore(float score) {
this.score = score;
}
public String toString()
{
return name+"的成绩是"+score;
}

}
public class ObjectStream implements Serializable {
public static void main(String[] args) throws IOException
{
try {
try {
FileOutputStream fos = new FileOutputStream("f://liyong.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Student student = new Student();
student.setName("liyong");
student.setScore(96.5f);
oos.writeObject(student);
} catch (FileNotFoundException ex) {
Logger.getLogger(ObjectStream.class.getName()).log(Level.SEVERE, null, ex);
}
FileInputStream fis = new FileInputStream("f://liyong.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Student s = (Student) ois.readObject();
System.out.println(s);
} catch (ClassNotFoundException ex) {
Logger.getLogger(ObjectStream.class.getName()).log(Level.SEVERE, null, ex);
}
}

}


JAVA高手帮忙!这个程序哪里出错。视频

相关评论:

相关主题精彩

版权声明:本网站为非赢利性站点,内容来自于网络投稿和网络,若有相关事宜,请联系管理员

Copyright © 喜物网