gpt4 book ai didi

Java 学生马克分类为奇数和偶数

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:04 25 4
gpt4 key购买 nike

创建类Student具有以下属性: rollNo, Name, Marks

创建类StudentDemomain方法。

main 中声明 5 个学生对象的数组方法。初始化这个数组。

在此类中声明另一个方法 – splitStudentArray 。此方法将采用 student数组和字符作为输入参数。

如果输入字符为“o”,此方法将返回分数为奇数的学生数组。如果输入字符是“e”,那么此方法将返回具有偶数值或分数的学生数组。如果指定了任何其他字符,它将返回空数组。

显示namemarks对于从 main 返回的数组方法。

最佳答案

您可以尝试以下StudentDemo.java:

public class StudentDemo {
public static void main(String[] args) {

Student st1 = new Student(1, "abhi", 200.5);
Student st2 = new Student(2, "maggie", 200);
Student st3 = new Student(3, "suraj", 107);
Student st4 = new Student(4, "naveen", 249);
Student st5 = new Student(5, "jajan", 101);

Student[] sts = { st1, st2, st3, st4, st5 };
System.out.println("splitStudentArray : " + java.util.Arrays.toString(splitStudentArray(sts, 'o')));
}

private static Student[] splitStudentArray(Student[] sts, char ch) {
if (ch == 'o') {
System.out.println("number is odd");
return oddArray(sts); // create function or insert for loop here
} else if (ch == 'e') {
System.out.println("number is even");
return evenArray(sts); // create function or insert for loop here
} else {
return null;
}
}

private static Student[] oddArray(Student[] sts) {
java.util.List<Student> list = new java.util.ArrayList<Student>();
for (Student student : sts) {
if (student.getMarks() % 2 != 0) {
list.add(student);
}
}
return list.toArray(new Student[0]);
}

private static Student[] evenArray(Student[] sts) {
java.util.List<Student> list = new java.util.ArrayList<Student>();
for (Student student : sts) {
if (student.getMarks() % 2 == 0) {
list.add(student);
}
}
return list.toArray(new Student[0]);
}

private static class Student {
private int rollNo;
private String Name;
private double Marks;

public Student(int rollNo, String name, double marks) {

this.rollNo = rollNo;
this.Name = name;
this.Marks = marks;
}

public int getRollNo() {
return rollNo;
}

public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}

public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}

public double getMarks() {
return Marks;
}

public void setMarks(double marks) {
Marks = marks;
}

@Override
public String toString() {
return "Student [rollNo=" + rollNo + ", Name=" + Name + ", Marks=" + Marks + "]";
}

}
}

并告诉我它是否适合您?

关于Java 学生马克分类为奇数和偶数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38176064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com