帮我看看这段程序哪错了?

xxslow 2008-03-25 05:51:32
就是输入进去学生名字,成绩,再求平均分
//StudentSystem
import javax.swing.JOptionPane;

class Student
{
int score;
String name;
static int counter,Max,Min,Sum;
public void GetName(String name)
{
this.name=name;
}
public void GetScore(int score)
{
this.score=score;
}
static void MaxAndMinScore(int score)
{
if (Max<score) Max=score;
if (Min>score) Min=score;
}
static void TotalScore(int current)
{
Sum=Sum+current;
}
static float CalAvgScore()
{
float a=Sum/counter;
return a;
}
}

public class StudentSystem
{
public static void main(String args[])
{
String str;
Student[] stu=new Student[10];
for (int i=0;i<10;i++)
{
str=JOptionPane.showInputDialog("输入学生的名字:");
stu[i].GetName(str);
str=JOptionPane.showInputDialog("输入学生的成绩:");
int score=Integer.parseInt(str);
stu[i].GetScore(score);
Student.MaxAndMinScore(score);
Student.TotalScore(score);
Student.counter++;
str=JOptionPane.showInputDialog("是否继续输入(Y/N)?");
if (str == "N") break;
}
System.out.println("输入的"+Student.counter+"个人的平均分是:"+Student.CalAvgScore());
System.out.println("最高分是"+Student.Max+",最低分是"+Student.Min);
}
}
...全文
97 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxslow 2008-03-26
  • 打赏
  • 举报
回复
我似乎知道哪错了,书上写着空对象不能用。。。String对象也不能用“=”赋值,不然成引用了。。。
谢谢你们来看啊!!
xxslow 2008-03-25
  • 打赏
  • 举报
回复
没有。。。
后来把Student类的构造函数写成Student(){ name=new String();}了,问题没解决
xxslow 2008-03-25
  • 打赏
  • 举报
回复
可以直接赋,不过就想在函数里完成
xxslow 2008-03-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xjy2004 的回复:]
运行时提示哪行什么错误呀,也方便别人读代码
[/Quote]

运行时第一个框可以出来,输入后就有异常了Exception in thread "main" java.lang.NullPoniterException...第42行
就是main里这句 stu[i].GetName(str);
AshEmissary 2008-03-25
  • 打赏
  • 举报
回复
Student[] stu=new Student[10
你的Student类有带参数的构造函数吗
simaping 2008-03-25
  • 打赏
  • 举报
回复
空指针,不明白你的student类都干什么了,似乎更复杂了, 直接赋不行么
xjy2004 2008-03-25
  • 打赏
  • 举报
回复
运行时提示哪行什么错误呀,也方便别人读代码

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧