就是输入进去学生名字,成绩,再求平均分
//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);
}
}
//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);
}
}
你的Student类有带参数的构造函数吗
运行时第一个框可以出来,输入后就有异常了Exception in thread "main" java.lang.NullPoniterException...第42行
就是main里这句 stu[i].GetName(str);
后来把Student类的构造函数写成Student(){ name=new String();}了,问题没解决
谢谢你们来看啊!!