问题描述
- java 新手,求检查改正代码
-
题目要求:
文件夹中给出的源程序Main.java,StuList.java,Student.java,填补其中的空缺内容。
要求:
(1)StuList.java和Student.java在同一个包中。
(2)Main.java在无名包中。
其中,Main.java中的类使用到StuList.java和Student.java中的成员变量或者成员方法。
本实验中的源程序可以完成如下任务:
创建学生链表,从键盘上输入学生信息加入学生链表,添加学生信息,删除学生信息,显示学生信息,按照学生成绩排序等。
我写的代码:
Main:import pzw.*;
import java.util.Scanner;public class Main {
public static void main(String[] args) { Scanner in=new Scanner(System.in); StuList list=new StuList(20); //学生链表可以容纳20个学生 System.out.println("要输入多少个学生的信息?"); int num=in.nextInt(); for(int i=0;i<num;i++) { System.out.println("请输入第"+(i+1)+"位学生的学号,姓名,数学、计算机成绩"); Student t=new Student(in.nextInt(),in.next(),in.nextDouble(),in.nextDouble()); list.addStu(t); } list.print(); list.addStu(new Student(1321,"liu",90,60)); list.addStu(new Student(1331,"guo",96,80)); list.addStu(new Student(1326,"niu",80,90)); list.removeStu(3); list.print(); System.out.println("按数学成绩排序:"); list.sort(1); list.print(); System.out.println("按计算机成绩排序:"); list.sort(2); list.print(); in.close(); }
}
StuList类:
package pzw;public class StuList
{
public Student []stu=new Student[30];int i=0,j=0,k=0,l=0,m=0,t=0; boolean yichu,kong; public StuList(int a) { j=a; } private boolean isOverflow() //辅助方法,判断数组是否溢出 { if(j<30) { yichu=false; } else { yichu=true; } return yichu; } public boolean isEmpty() //判断数组是否为空 { if(stu==null) { kong=true; } else{ kong=false; } return kong; } public boolean addStu(Student x) //添加学生信息 { if(isOverflow()) return false; else { stu[k]=x; k++; return true; } } public Student removeStu(int index) //删除指定位置的学生信息 { int p; if(isEmpty()) { System.out.println("学生表空的~~~删除失败~~~~"); return null; } Student temp=stu[index-1]; for(p=index;p<j;p++) { stu[p]=stu[p+1]; } return temp; } public void sort(int x) //按照成绩排序,参数1按数学排序,参数2按计算机排序 { if(x==1) { for(i=0;i<j-1;i++) { for(m=0;m<j-1;m++) { if(stu[m].sx<stu[m+1].sx) { Student a; a=stu[m]; stu[m]=stu[m+1]; stu[m+1]=a; } } } } else if(x==2) { for(i=0;i<j-1;i++) { for(m=0;m<j-1;m++) { if(stu[m].jsj<stu[m+1].jsj) { Student a; a=stu[m]; stu[m]=stu[m+1]; stu[m+1]=a; } } } } } public void print() { for(l=0;l<j;l++) { System.out.println(stu[l].xh+ stu[l].xm+ stu[l].sx+ stu[l].jsj); } }
}
Student:
package pzw;public class Student {
int xh=0;
String xm=null;
double sx=0;
double jsj=0;public Student(int a,String b,double c,double d) { xh=a; xm=b; sx=c; jsj=d; } public int getxh() { return xh; } public String getxm() { return xm; } public double getsx() { return sx; } public double getjsj() { return jsj; }
}
附题目给出的程序:
Student.java:
【包的声明】public class Student
{
【成员变量的声明】【构造方法】 【获取学号】 【设置学号】 【获取姓名】 【设置姓名】 【获取数学成绩】 【设置数学成绩】 【获取计算机成绩】 【设置计算机成绩】 【输出学生信息】
}
StuList.java:
【包的声明】
public class StuList
{
【学生信息声明】
private int length; //实际容量【构造方法】 private boolean isOverflow() //辅助方法,判断数组是否溢出 { 【代码】 } public boolean isEmpty() //判断数组是否为空 { 【代码】 } public boolean addStu(Student x) //添加学生信息 { if(isOverflow()) return false; 【代码】 return true; } public Student removeStu(int index) //删除指定位置的学生信息 { if(isEmpty()) { System.out.println("学生表空的~~~删除失败~~~~"); return null; } Student temp=stu[index-1]; 【代码】 return temp; } public void sort(int x) //按照成绩排序,参数1按数学排序,参数2按计算机排序 { if(x==1) { 【代码】 } else if(x==2) { 【代码】 } } public void print() { 【代码】 }
}
主函数:
【包的引用】
public class Main
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
StuList list=new StuList(20); //学生链表可以容纳20个学生
System.out.println("要输入多少个学生的信息?");
int num=in.nextInt();
for(int i=0;i<num;i++)
{
System.out.println("请输入第"+(i+1)+"位学生的学号,姓名,数学、计算机成绩");
Student t=new Student(in.nextInt(),in.next(),in.nextDouble(),in.nextDouble());
list.addStu(t);
}
list.print();list.addStu(new Student(1321,"liu",90,60)); list.addStu(new Student(1331,"guo",96,80)); list.addStu(new Student(1326,"niu",80,90)); list.removeStu(3); list.print(); System.out.println("按数学成绩排序:"); list.sort(1); list.print(); System.out.println("按计算机成绩排序:"); list.sort(2); list.print(); in.close(); }
}
解决方案
检查了下你的程序,发现运行print时报空指针异常。主要就是你的StudetnList类的代码中定义的int成员太多,使用时各个成员作用不明确导致代码逻辑错误。
其实你只需要一个下标就可以了,这个下标标识当前数组可以新添加操作的元素的下标(即下次添加元素时元素的位置),如果添加一个元素,则放在该下标处则同时该标识下标增1;删除元素时,该标识下标减一。
修正了你的代码,基本逻辑不变,Student和Main类都是一样的,仅仅修正了名称,以有意义的单词替代了。
Student类:(重写toString方法,输出类的信息)
public class Student {
//学生学号
int id = 0;
//学生姓名
String name = null;
//数学成绩
double mathScore = 0;
//计算机成绩
double computerScore = 0;
public Student(int a, String b, double c, double d) {
id = a;
name = b;
mathScore = c;
computerScore = d;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getComputerScore() {
return computerScore;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
}
//重写toString方法,输出Student类的信息
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", mathScore=" + mathScore + ", computerScore=" + computerScore
+ "]";
}
}
StuList类:
public class StuList {
private int length; // 实际容量
private Student[] stu;// 实际存储容器
private int cursorIndex;// 当前可进行数据操作的下标
// 构造函数,指定容量
public StuList(int length) {
if (length < 0) {
throw new IllegalArgumentException("学生容器容量必须非负");
}
// 容量为0,不作处理
if (length == 0) {
return;
}
// 初始化容器数组
this.length = length;
stu = new Student[length];
cursorIndex = 0;
}
// 如果数组可操作下标等于容器容量,说明容器已满
private boolean isOverflow() // 辅助方法,判断数组是否溢出
{
return cursorIndex == length;
}
// 如果数组可操作下标为0说明数组中没有数据
public boolean isEmpty() // 判断数组是否为空
{
return cursorIndex == 0;
}
// 未作判空操作,允许插入null对象
public boolean addStu(Student x) // 添加学生信息
{
if (isOverflow()) {
System.out.println("容器已满,添加操作失败.");
return false;
}
// 加入student
stu[cursorIndex] = x;
// 下标往前移动,以便下一次进行增加操作
cursorIndex++;
return true;
}
//使用了数组,所以删除元素时,后面的元素需要顺次往前移动一个位置
public Student removeStu(int index) // 删除指定位置的学生信息
{
if (isEmpty()) {
System.out.println("学生表空的~~~删除失败~~~~");
return null;
}
if (index > cursorIndex) {
System.out.println("删除下标超出学生表存储的最大数据下标~~~删除失败~~~~");
return null;
}
// 执行删除操作
Student temp = stu[index - 1];
//如果是删除最后一个元素,则不需挪动
if(index!=cursorIndex){
//删除元素后面的所有元素往回移动一个位置
for(int i = index-1;i<cursorIndex;i++)
{
stu[i] = stu[i+1];
}
}
// 最后一个元素下标处元素设置为null
stu[cursorIndex - 1] = null;
// 元素操作下标往回移动
cursorIndex--;
return temp;
}
public void sort(int x) // 按照成绩排序,参数1按数学排序,参数2按计算机排序
{
// 使用简单的冒泡排序
Student temp = null;
for (int i = cursorIndex - 1; i > 0; --i) {
boolean isSort = false;
for (int j = 0; j < i; ++j) {
Student indexj1 = stu[j + 1];
Student indexj = stu[j];
// 按数学排序
boolean compareFlag = false;
if (x == 1) {
compareFlag = indexj1.getMathScore() < indexj.getMathScore();
} else if (x == 2) {
compareFlag = indexj1.getComputerScore() < indexj.getComputerScore();
}else{
System.out.println("不支持参数值"+x);
throw new IllegalArgumentException("不支持参数值"+x);
}
if (compareFlag) {
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
isSort = true;
}
}
// 如果一次内循环中发生了交换,那么继续比较;如果一次内循环中没发生任何交换,则认为已经排序好了。
if (!isSort)
break;
}
}
public void print() {
if (isEmpty()) {
System.out.println("学生表数据为空.");
return;
}
// 打印数据
for (int i = 0; i < cursorIndex; i++) {
Student student = stu[i];
if (student != null) {
System.out.println("第" + (i + 1) + "个学生信息:" + student.toString());
} else {
System.out.println("第" + (i + 1) + "个位置插入了空对象,学生信息为空");
}
}
}
}
Main类不变。注意删除元素时删除元素以后的数组各个元素往前挪动一个位置;添加元素时可以允许添加null对象。
解决方案二:
Java静态代码检查工具笔记
Java静态代码检查工具
如何写静态代码检查规则(JAVA)
解决方案三:
int score;
{
sid;
sname;
math;
};
void caluscore(student stu[],int length)
{
for (int i=0;i
stu[i].score=stu[i].chinese+stu[i].math;
}
int max(student stu[],int length)
{
int maxvalue=stu[0].score;
int maxstu=0;
for(int i=0;i
if (stu[i].score>maxvalue)
{
maxvalue=stu[i].score;
maxstu=i;
}
return maxstu;
}
void main()
{
student stu[3];
for(int i=0;i
cin>>stu[i].sid>>stu[i].sname>>stu[i].math>>stu[i].chinese;
caluscore(stu,3);
cout<<max(stu,3)+1<<end;
;
}
哪里出问题了 求大神