有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用数组,一个用来保存元素数组的值

   public class Student implements Comparable<Student> {
        String fName;
        String sName;
        String subject[];
        int subjectValuation;
         public Student(String fName, String sName, String[] subject, int subjectValuation) {
            this.fName = fName;
            this.sName = sName;
            this.subject = subject;
            this.subjectValuation = subjectValuation;
        }

        public String[] getSubject() {
            return subject;
        }

        public String getfName(){
            return fName;

        }
        public String getsName(){
            return sName;
        }

        public void setSubject(String[] subject) {
            this.subject = subject;
        }

        public int getSubjectValuation(){
            return subjectValuation;
        }
        public void setSubjectValuation(int subjectValuation){
            this.subjectValuation=subjectValuation;
        }
        public String toString(){
            return " fname "+getfName()+ " sname " +getsName()+ " subjects "+subject+ " valuation "+subjectValuation;
        }
        public boolean equals(Object o){
            if(o!=null && o instanceof Student){
                Student s=(Student) o;
                if(this.getfName().equalsIgnoreCase(s.fName) && this.getsName().equalsIgnoreCase(s.sName))
                    return true;
                else
                    return false;
            }
            return false;
        }
        public int compareTo(Student o){
            return this.subjectValuation-o.subjectValuation;
        }


        }






              import java.util.ArrayList;
              import java.util.Collections;
              import java.util.Iterator;

               public class Catalogue {
              private ArrayList<Student> s=new ArrayList<>();
              //performing 1.
            void add(String firstName,String secondName){
                s.add(new Student(firstName,secondName,[0],0));
            }
              //performing 2
            void add2(String firstname,String secondName,String subject[],int valuation){
                Student accSearch=search(firstname,secondName);
                if(accSearch!=null) {
                    accSearch.setSubject(subject);
                    accSearch.setSubjectValuation(valuation);
                }
                else{
                    System.out.println("couldn't find it");
                }

            }
                 //performing 3
            void delete(String firstName,String secondName){
                Student n=new Student(firstName,secondName,[0],0);
                    while(s.contains(n)) {
                        s.remove(n);
                    }

                }
                   //searching my Student
            public Student search(String firstName,String secondName){
                for(Student m:s){
                    if(m.getfName().equalsIgnoreCase(firstName) && m.getsName().equalsIgnoreCase(secondName))
                        return m;
                }
                return null;
            }
                public void listInformation(String firstName,String secondName){
                  for(Student m:s){
                      if(m.getfName().equalsIgnoreCase(firstName) && 
              m.getsName().equalsIgnoreCase(secondName))
                        System.out.println(m);

                }
            }

            void printall(){
                for(Student m:s){
                    System.out.println(m);
                }
            }
                  //performing 4.
            void listValuation(String firstName,String secondName) {
                Student searchingFor = search(firstName, secondName);
                Iterator<Student> i = s.iterator();
                while (i.hasNext()) {
                    Student s = i.next();
                    if (s.getfName().equalsIgnoreCase(firstName) && s.getsName().equalsIgnoreCase(secondName))
                        System.out.println(s.getSubjectValuation());
                }
             ``}
                  //the average not sure how am i going to do it
             void average(String firstName,String secondName){
                Student s=search(firstName,secondName);


            }
             void sort(){
                Collections.sort(s);



             }
           }
public static void main(String[] args) {
        Catalogue c=new Catalogue();
        c.add("a","b");
        c.add("b","c");
        c.add("d","f");
        c.add("a","b");
       // c.listInformation("a","b");
        c.delete("a","b");
        System.out.println("+");
        c.printall();
        c.add2("d","f","math",9);
        c.printall();
        c.add2("f","a","asdsdfd",1);
        System.out.println("++");
        c.printall();
        System.out.println("++++");
       c.listValuation("d","f");
   }
}

上面的课是我的目录课

我必须执行以下任务:

  1. 添加一名学生
  2. 为学生添加科目和笔记
  3. 删除一个学生
  4. 在学生姓名后搜索并打印每个科目的每个分数
  5. 在学生姓名后搜索并计算科目平均数

我不知道如何为学生分配一系列科目和分数,例如,我有一个名叫乔恩·乔恩的学生,他可以有多个科目,比如数学1、数学2、数学3。。。。像Math1等级是1 Mah2等级是2

第五个。没有实现,但我更感兴趣的是如何实现主题和注释的数组


共 (1) 个答案

  1. # 1 楼答案

    如果我没弄错你的问题,你是在问如何把科目和成绩结合起来,然后把它们放到一个数组中。 解决这个问题的一种方法是实现一个单独的类Subject,它也会得到attribute的分数

    public class Subject {
        private String subject;
        private float grade;
    
        //getter and setter methods
    }
    

    你可以在你的类中使用这个类Student

    public class Student implements Comparable<Student> {
            String fName;
            String sName;
            Subject subject[]; //array which can contain multiple subjects. each 
                               //entry got two attributes: the subject itself as String; the grade
    
            //your code
    }