hclyyds
HCLYYDS
2021-06-14 14:00
采纳率: 100%
浏览 39
已采纳

eclipse java

package bigWork;

public class teachers extends people {
	private course[] courselist=new course[1];
	public teachers(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}
	public void addcourse(String course){
		(this.courselist[0]).setCourseName(course);;
	}
	@Override
	public String getName() {
		return name;
	}
	public void setName(String name) {
		System.out.println("请输入老师姓名:");
		this.name = name;
	}
	public course[] getCourselist() {
		return courselist;
	}
	public void setCourselist(course[] courselist) {
		this.courselist=courselist;
	}
	public String getcourse(){
		return courselist[0].getCourseName();
	}
}

package bigWork;
import java.util.Scanner;
import java.util.ArrayList;
public class studentandteacherMannager {
		        //创建集合对象,用于存储学生数据
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
				System.out.println("请输入本学期课程(五门课程)");
				System.out.println("课程一:");
				course c1=new course(scanner.nextLine());
				System.out.println("课程二:");
				course c2=new course(scanner.nextLine());
				System.out.println("课程三:");
				course c3=new course(scanner.nextLine());
				System.out.println("课程四:");
				course c4=new course(scanner.nextLine());
				System.out.println("课程五:");
				course c5=new course(scanner.nextLine());
		        ArrayList<students> array1 = new ArrayList<>();
		        ArrayList<teachers> array2 = new ArrayList<>();
		        //用循环完成再次回到主界面
		        while (true) {
		            // 1. 用输出语句完成主界面的编写
		            System.out.println("---------欢迎来到学生老师管理系统------------");
		            System.out.println("1 添加学生和所学科目");
		            System.out.println("2 删除学生");
		            System.out.println("3 查看所有学生和成绩");
		            System.out.println("4 添加老师");
		            System.out.println("5 删除老师");
		            System.out.println("6 查看所有老师和所教科目");
		            System.out.println("7 退出");
		            System.out.println("-------------------------------------");

		            // 2. 用Scanner实现键盘录入数据
		            Scanner s = new Scanner(System.in);
		            int line = s.nextInt();


		            //3. 用switch语句完成操作的选择
		            switch (line) {
		                case 1:
		                    //System.out.println("添加学生");
		                    addstudents(array1);
		                    break;
		                case 2:
		                    //System.out.println("删除学生");
		                    deletestudents(array1);
		                    break;
		                case 3:
		                    //System.out.println("查看所有学生");
		                    findAllstudents(array1);
		                    break;
		                case 4:
		                    //System.out.println("添加老师");
		                    addteachers(array2);
		                    break;
		                case 5:
		                    //System.out.println("删除老师");
		                    deleteteachers(array2);
		                    break;
		                case 6:
		                    //System.out.println("查看所有老师");
		                    findAllteachers(array2);
		                    break;
		                case 7:
		                    System.out.println("谢谢使用");
		                    System.exit(0);
		            }
		        }
		    }
	//查询老师和老师所教科目
		    private static void findAllteachers(ArrayList<teachers> array2) {
					// TODO Auto-generated method stub
		    	for(int i=0;i<array2.size();i++) {
		    	System.out.println("老师姓名:"+array2.get(i).name+"所教科目"+array2.get(i).getcourse());
				}
		    }
			private static void deleteteachers(ArrayList<teachers> array2) {
					// TODO Auto-generated method stub
					Scanner scanner=new Scanner(System.in);
					System.out.println("请输入要删除老师的姓名:");
					String a=scanner.nextLine();
					for(int i=0;i<array2.size();i++) {
						if(array2.get(i).name==a) {
							array2.remove(i);
							System.out.println("删除成功!");
						}else {
							System.out.println("未查询到该老师,删除失败");
						}
					}
				}


			private static void addteachers(ArrayList<teachers> array2) {
				Scanner s=new Scanner(System.in);
		// TODO Auto-generated method stub
					System.out.println("请输入三位老师的姓名:");
					teachers t0=new teachers(s.nextLine());
					teachers t1=new teachers(s.nextLine());
					teachers t2=new teachers(s.nextLine());
					System.out.println("请输入三位老师所教科目:");
					t0.addcourse(s.nextLine());;
					t1.addcourse(s.nextLine());
					t2.addcourse(s.nextLine());
					array2.add(0, t0);
					array2.add(1, t1);
					array2.add(2, t2);
					
	}


			private static void findAllstudents(ArrayList<students> array1) {
					// TODO Auto-generated method stub
					
				}
			//定义一个方法,用于添加学生信息
		    private static void deletestudents(ArrayList<students> array1) {
		// TODO Auto-generated method stub
		
	}


			private static void addstudents(ArrayList<students> array1) {
		// TODO Auto-generated method stub
				Scanner s=new Scanner(System.in);
		// TODO Auto-generated method stub
					System.out.println("请输入五位学生的姓名:");
					students t0=new students(s.nextLine());
					students t1=new students(s.nextLine());
					students t2=new students(s.nextLine());
					students t3=new students(s.nextLine());
					students t4=new students(s.nextLine());
					System.out.println("请输入五位学生所学习的科目(每位学生需要报名三门课):");
					t0.addcourse(s.nextLine(),s.nextLine(),s.nextLine());
					t1.addcourse(s.nextLine(),s.nextLine(),s.nextLine());
					t2.addcourse(s.nextLine(),s.nextLine(),s.nextLine());
					t3.addcourse(s.nextLine(),s.nextLine(),s.nextLine());
					t4.addcourse(s.nextLine(),s.nextLine(),s.nextLine());
					array1.add(0, t0);
					array1.add(1, t1);	
					array1.add(2, t2);
					array1.add(3, t3);
					array1.add(4, t4);
	}
}


这个程序的teachers类里这个setCoursesname出什么错误了?为什么不行啊

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • hclyyds
    HCLYYDS 2021-06-14 14:29
    已采纳
    package bigWork;
    
    public class course {
    	private String courseName;
    	public score[] score;
    	public course(String courseName) {
    		this.courseName = courseName;
    	}
    	public String getCourseName() {
    		return courseName;
    	}
    	public void setCourseName(String courseName) {
    		this.courseName = courseName;
    	}
    }
    
    点赞 评论
  • technologist_30
    CSDN专家-张老师 2021-06-14 14:02

    你courselist[0]为null,你检查下数据

    点赞 1 评论
  • u010560510
    魂兮呀 2021-06-14 21:27

    没有初始化对象,你只是初始化了个这个类型的数组

    点赞 评论
  • weixin_56010015
    XIKK'雪 2021-06-17 11:33

    不太懂

    点赞 评论
  • QA_Assistant
    有问必答小助手 2021-06-17 13:52

    您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps: 问答会员年卡【8折】购 ,限时加赠IT实体书,即可 享受50次 有问必答服务,了解详情>>>https://t.csdnimg.cn/RW5m

    点赞 评论

相关推荐