任何人都可以帮我解决这些问题吗? [英] Can anyone help me to solve these problem ?

查看:73
本文介绍了任何人都可以帮我解决这些问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我java项目的一部分。有没有人对我的问题有任何疑问,学生班的删除课程方法存在问题。它无法正常工作。因为当我想从listLesson中删除课程时,输出是没有存在!,但实际上我已经创建了一个课程对象并将该课程添加到listLesson。实际上我不知道是什么问题。可能它与addlesson方法有关,我认为它不会给listLesson添加课程,所以当我要删除它时,输出是No Exist!。如果您对解决此问题有任何想法,请告诉我。谢谢。

This is my part of java project.Does anyone have any idea about my question that there are problems with delete lesson method in student class.It does not work correctly.Because when i want to delete lesson from listLesson ,the output is "No Exist!",but actually i have created a lesson object and added that lesson to listLesson.Really i dont know what is the problem.May be it is related with addlesson method,i think it does not add lesson to listLesson,so when i wan to delete it is output is" No Exist!".Please,if you have any idea about solving this problem,let me know.Thanks in advance :)

public class student {
    private String name;

    private int id;
    private lesson[]listLesson;
    private int size;
    private int number=0;
    
   
    public student(String name){
        this.name=name;
        listLesson = new lesson[size];
        this.size=5;
    }
    public boolean isExist(lesson l){
        for(int i=0; i < number; i++)
            if(listLesson[i].equals(l))
                if(listLesson[i].getNote()>=60)
                    return true;
        return false;
    }
    public void addLesson(lesson l){     
        if(!this.isExist(l)){
            if(number==size){
               lesson[] newList = new lesson[size+1];
               for(int i=0; i < size; i++){
                   newList[i]=listLesson[i];
               }
               listLesson=newList;
               size++;     
                // System.out.println(listLesson);
            }
            
            listLesson[number]= l;
            number++;
       }
    
        else
            System.out.println("Already passed :"+ l);
            
    }

    public student(String name, int id) {
        this.name = name;
        this.id = id;
    }
   
    public void deleteLesson(lesson l){
        int index=existList(l);
        if(index!=-1){
            lesson []newL=new lesson[size-1];
            for(int i=0;i<index;i++){
                newL[i]=listLesson[i];
            }
            for(int i=index+1;i<size-1;i++){
                newL[i]=listLesson[i+1];
             
            }
               listLesson=newL;
                size--;           
                
        }else{
            System.err.println("No Exist!");
        }
        
    }
    
        public int existList(lesson l){
        for(int i=0;i<number;i++){
            if(listLesson[i].equals(l.getCode())){
                return i;
            }
        }
        return -1;
    }

    public lesson[] getListLesson() {
        return listLesson;
    }





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

    public void setId(int id) {
        this.id = id;
    }

    public void setListLesson(lesson l) {
        this.listLesson=new lesson[size];
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }

    public boolean equals(student s) {
        if (this.id == s.id && this.name.equals(s.name)) {
            return true;
        }
        return false;
    }

 


    
   public String toString() {        
 String res="{";
  int i;
  for(i=0;i<size-1;i++){
      if(listLesson.length==0)
          res+="-, ";
      else
      res+=listLesson[i]+", ";
      
  }
  if(listLesson.length==0)
      res+="-";
  else
  res+=listLesson[i];
 
  return "student{" + "name=" + name + ", id=" + id + res+"}";
          }

};





我的尝试:



我创建了一些课程。但是有一些问题。如果有人可以帮助我,请告诉我。



What I have tried:

I have created some classes. But there are some problems.If someone can help me,please tell me.

推荐答案

在你的删除方法你调用 existList 执行以下操作:

In your delete method you call existList which does the following:
public int existList(lesson l){
    for(int i=0;i<number;i++){
        if(listLesson[i].equals(l.getCode())){
            return i;
        }
    }
    return -1;
}



getCode()返回课程对象?如果没有,那么等于测试很可能会失败。您还声明此方法返回 int 但您尝试在成功时返回课程


Does getCode() return the lesson object? If not then the equals test will most likely fail. You also declare this method to return an int but you try to return a lesson on success.


这篇关于任何人都可以帮我解决这些问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆