public class Student { private int num; private String name; private String sex; private String hobbies; private int[] score = new int[50]; public void add() { System.out.println("学生成绩添加"); // System.out.println("请输入5个成绩"); // for(int i=0;i<5;i++){ // Scanner scanner = new Scanner(System.in); // int s = scanner.nextInt(); // score[i]=s; // } System.out.println("请输入成绩:"); Scanner scanner = new Scanner(System.in); int s = scanner.nextInt(); int index = 0; // 记录数组目前有多少个元素 for (int s2 : score) { if (s2 != 0) { index++; } } this.score[index] = s; } public void delete() { System.out.println("学生成绩删除"); System.out.println("要删除哪个分数?"); Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int i = n; i < n - 1; i++) { score[i - 1] = score[i]; } int[] temp = new int[5]; int j = 0; for (int i = 0; i < 5; i++) { if (i != n - 1) { temp[j] = score[i]; j++; } else { temp[j] = score[i + 1]; j++; i++; } } score = temp; } public void update() { System.out.println("学生成绩修改"); System.out.println("要修改哪个分数?改为多少分?"); Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int s = scanner.nextInt(); score[n - 1] = s; } public void showScore() { // System.out.println("学生成绩查询"); System.out.println("学生成绩为:"); for (int s : score) { if (s != 0) { System.out.println(s); } } } public void showHobbies() { System.out.println("我的兴趣是:" + this.hobbies); } public Student(int num, String name, String sex, String hobbies) { super(); this.num = num; this.name = name; this.sex = sex; this.hobbies = hobbies; } public void setScore(int[] score) { this.score[0] = score[0]; this.score[1] = score[1]; this.score[2] = score[2]; this.score[3] = score[3]; this.score[4] = score[4]; } public static void main(String[] args) { Student student = new Student(1, "张三", "男", "篮球,羽毛球,游泳"); int[] s = { 91, 92, 93, 94, 95 }; student.setScore(s); // student.showHobbies(); student.add(); // student.delete(); // student.update(); student.showScore(); } }
在学生类中添所学课程的成绩表(一维数组),并处理成绩(增删查改)
猜你喜欢
转载自blog.csdn.net/qq_39403545/article/details/80233315
今日推荐
周排行