鱼C论坛

 找回密码
 立即注册
查看: 2158|回复: 2

数组与类

[复制链接]
发表于 2022-5-4 15:17:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
【问题描述】设计一个学生类,有学号、姓名、性别、年龄和数学、英语、程序三门课程成绩(整型)。创建有三个学生的对象数组,输出各学生的信息。

【输入形式】按学号、姓名、性别、年龄、数学、英语、程序的顺序输入,空格隔开,一人一行。

【输出形式】student:?,?,?,?,?,?,?   输出学生信息的顺序与输入顺序相同,数据之间用“,”隔开,一人一行
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-4 16:05:30 | 显示全部楼层
  1. class Subject:
  2.     def __init__(self, math, eng, progm):
  3.         self.math = math
  4.         self.eng = eng
  5.         self.progm = progm

  6. class Student:
  7.     def __init__(self, ID, name, gender, age, score):
  8.         self.ID = ID
  9.         self.name = name
  10.         self.gender = gender
  11.         self.age = age
  12.         self.score = score
  13.         self.data = [self.ID, self.name, self.gender, self.age, self.score]
  14.    
  15.     def __iter__(self):
  16.         self.current_index = 0
  17.         return self

  18.     def __next__(self):
  19.         if self.current_index < len(self.data):
  20.             x = self.data[self.current_index]
  21.             self.current_index += 1
  22.             return x
  23.         raise StopIteration


  24. students = [_ for _ in range(3)]

  25. for i in range(3):
  26.     ID, name, gender, age, m, e, p = map(str, input().split())
  27.     score = Subject(m, e, p)
  28.     students[i] = Student(ID, name, gender, age, score)

  29. for stu in students:
  30.     ID, name, gender, age, score = stu
  31.     print(ID, name, gender, age, score.math, score.eng, score.progm)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-4 19:31:38 | 显示全部楼层



学生类 Student.java:

  1. public class Student {
  2.     private Integer id;
  3.     private String name;
  4.     private String gender;
  5.     private Integer age;
  6.     private Integer math;
  7.     private Integer english;
  8.     private Integer program;

  9.     public Student() {
  10.     }

  11.     public Student(Integer id, String name, String gender, Integer age, Integer math, Integer english, Integer program) {
  12.         this.id = id;
  13.         this.name = name;
  14.         this.gender = gender;
  15.         this.age = age;
  16.         this.math = math;
  17.         this.english = english;
  18.         this.program = program;
  19.     }


  20.     public Integer getId() {
  21.         return id;
  22.     }

  23.     public void setId(Integer id) {
  24.         this.id = id;
  25.     }

  26.     public String getName() {
  27.         return name;
  28.     }

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

  32.     public String getGender() {
  33.         return gender;
  34.     }

  35.     public void setGender(String gender) {
  36.         this.gender = gender;
  37.     }

  38.     public Integer getAge() {
  39.         return age;
  40.     }

  41.     public void setAge(Integer age) {
  42.         this.age = age;
  43.     }

  44.     public Integer getMath() {
  45.         return math;
  46.     }

  47.     public void setMath(Integer math) {
  48.         this.math = math;
  49.     }

  50.     public Integer getEnglish() {
  51.         return english;
  52.     }

  53.     public void setEnglish(Integer english) {
  54.         this.english = english;
  55.     }

  56.     public Integer getProgram() {
  57.         return program;
  58.     }

  59.     public void setProgram(Integer program) {
  60.         this.program = program;
  61.     }

  62.     @Override
  63.     public String toString() {
  64.         return "student:" + id +
  65.                 "," + name +
  66.                 "," + gender +
  67.                 "," + age +
  68.                 "," + math +
  69.                 "," + english +
  70.                 "," + program;
  71.     }
  72. }
复制代码


输出学生信息类 PrintStudentInfo.java:

  1. import java.util.Scanner;

  2. public class PrintStudentInfo {
  3.     public static void main(String[] args) {
  4.         Scanner input = new Scanner(System.in);
  5.         Student[] students = new Student[3];
  6.         for (int i = 0; i < students.length; i++){
  7.             String str = input.nextLine();
  8.             String[] data = str.split(" ");
  9.             Student stu = new Student(
  10.                     Integer.parseInt(data[0]),
  11.                     data[1],
  12.                     data[2],
  13.                     Integer.parseInt(data[3]),
  14.                     Integer.parseInt(data[4]),
  15.                     Integer.parseInt(data[5]),
  16.                     Integer.parseInt(data[6])
  17.             );
  18.             students[i] = stu;
  19.         }

  20.         for (Student student : students){
  21.             System.out.println(student);
  22.         }
  23.     }
  24. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 03:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表