创建学生数组,并使用值

时间:2017-11-01 17:33:52

标签: java arrays

我正在尝试做什么:

输入1到5之间的数字

  1. 退出计划

  2. 向4学生数组添加详细信息(ID,姓名,assignment_mark1,assignment_mark2,考试)

  3. 输出学生的详细信息

  4. 计算并输出学生的平均整体分数(assignment_mark1,assignment_mark2,examination)/ 3

  5. 输入学生ID并显示输入学生详细信息

  6. 我不能使用数组列表

    我的对象类:

     public class Student{
     Scanner keyboard = new Scanner(System.in);
     private String name;
     private int ID;
     private float assignment_mark1;
     private float assignment_mark2;
     private float examination;
    
     StudentDetail(){ //default constructor
     this.name= "default";
     this.ID =0;
     this.assignment_mark1 = 0;
     this.assignment_mark2 = 0;
     this.examination = 0;
     }
    
     Student(String name, int ID, float assignment_mark1, float 
    assignment_mark2, 
     float examination){   
     this.name = name;
     this.ID= ID;
     this.assignment_mark1= assignment_mark1;
     this.assignment_mark2 = assignment_mark2;
     this.examination = examination;
    
     }
    
      /*
     get and set methods here
      */
    
     quit(){ //quit program }
    
     addDetailsToAllStudent();  
    
     outputAllDetails();
    
     computeAverageMarks(double assignment1, double assignment2, double 
    examination, double practical_work) {  
     double total=0;
    total = (assignment1 + assignment2 + examination) / 3.0;
    return  total;
    
     displayStudentDetail();
    

    静态类:

    public static void main(String[] args) {
      Scanner keyboard = new Scanner(System.in);
    
    
      Student[] array = new Student[4];//
      boolean isRunning = true;
      int choice = 0;
      while(isRunning){
    
          System.out.println("1. Quit ");
          System.out.println("2. Add  Details to all students ");
          System.out.println("3. Output all student details");
          System.out.println("4. compute average student marks");
          System.out.println("5. Display student details based on ID");
          choice = keyboard.nextInt();
    
       while(choice>0 && choice<=5)
       {
          if (choice == 1)  //quit
          {
             System.exit(0);
          }
          else if (choice == 2) //Add  Details to all students
          { 
    
              for(int i=0; i< array.length ; i++)
             {
              array[i] = new StudentDetail();
    
              System.out.println("enter student"+(i +1) +":");
    
               System.out.println("name");
              String first_name = keyboard.nextLine();
              array[i].setName(name);
    
    
               System.out.println("Student id");
              int ID= keyboard.nextInt();
              array[i].setStudent_id(ID);
    
    
               System.out.println("get Assignment1");
              int getAssignment1 = keyboard.nextInt();
              array[i].setAssignment1(getAssignment1);
    
               System.out.println("get Assignment2 ");
              int getAssignment2 = keyboard.nextInt();
              array[i].setAssignment2(getAssignment2);
    
           System.out.println("get Examination");
              int getExamination = keyboard.nextInt();
              array[i].setExamination(getExamination);
        }
       }
    
         else if (choice == 3) //Output all student details
           {
               for(int i=0; i< array.length ; i++)
              {
                  System.out.println(array[i].getName());
                  System.out.println(array[i].getStudent_id());
                  System.out.println(array[i].getAssignment1());
                  System.out.println(array[i].getAssignment2());
                  System.out.println(array[i].getExamination());
              }
          }
    
          else if (choice == 4) //compute average student marks
          {
             for(int i=0; i< array.length ; i++)
             {
             double temp = array[i].getAssignment1();
             double temp2=  array[i].getAssignment2();
             double temp3= array[i].getPractical_work();
             double temp4 =  array[i].getExamination();
    
    
             System.out.println(array[i].averageOverallMark(temp, 
               temp2,temp3,temp4));
             }
          }
          else if (choice == 5) // Display student details based on ID
          {
              numberOfStudentAchieveMark();
          }
    

    我的问题:

    1. 通常在创建对象时我会做这样的事情:“Student std1 = new Student();” 然后,如果我想运行方法quit(),它将是std1.quit(); 但是,因为我正在创建一个数组,我将如何运行方法quit()或任何其他方法;
    2. -------------已解决-------------------

1 个答案:

答案 0 :(得分:0)

对于你的问题1:你可以像上面一样排列[i] .getName()。 对于你的问题2:因为你使用私人浮动来获得任务分数。你需要完成你的getter和setter。 对于您的问题3:在步骤2中创建设置后,您可以在静态类中创建的Student对象上调用该setter。