多个输入的数组

时间:2016-12-17 14:55:40

标签: java arrays

有人可以帮助我使用java中的程序,该程序将接受10个学生的姓名,他们的matric数字以及他们的10个课程的分数,并且还使用我使用的代码确定他们的成绩,但它不是数组< / p>

import java.util.*;
public class GPACALCULATOR {

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int Unit,Tunit=0;
    double Gp,Tgp=0;
    int Score;
    double Sgp = 0;
    double Gpa= 0;
    String Course, Grade, Name, Mnum;
    System.out.println("PLEASE INPUT YOUR NAME");
    Name= s.next();
     System.out.println("PLEASE INPUT THE LAST TWO DIGITS OF YOUR MATRIC NUMBER");
    Mnum= s.next();
    System.out.println("HOW MANY COURSE ARE YOU OFFERING");
    int C= s.nextInt();



    for (int X= 0; X<C; X++)
    {
        System.out.println("ENTER COURSE CODE");
        Course= s.next();
        System.out.println("ENTER HE COURSE UNIT");
        Unit= s.nextInt();
        System.out.println("ENTER THE COURSE SCORE");
        Score= s.nextInt();
        System.out.println(" ");

        if (Score>=70)
        {
            Grade= "A";
            Gp = 4.0;
            Tgp= Gp * Unit;
            System.out.println("COURSE " + "SCORE " + "GRADE " + "GP ");
            System.out.println(" ");
            System.out.println(Course+" "+Unit+ " " +Score+" "+Grade+" "+Gp+" ");
            System.out.println(" ");

        }


        else if (Score>=65)
        {
            Grade= "AB";
            Gp = 3.5;
            Tgp= Gp * Unit;
            System.out.println("COURSE " + "SCORE " + "GRADE " + "GP ");
            System.out.println(" ");
            System.out.println(Course+" "+Unit+ " " +Score+" "+Grade+" "+Gp+" ");
            System.out.println(" ");

        } 

       else if (Score>=60)
        {
            Grade= "B";
            Gp = 3.0;
            Tgp= Gp * Unit;
            System.out.println("COURSE " + "SCORE " + "GRADE " + "GP ");
            System.out.println(" ");
            System.out.println(Course+" "+Unit+ " " +Score+" "+Grade+" "+Gp+" ");
            System.out.println(" ");

        } 

        else if (Score>=55)
        {
            Grade= "C";
            Gp = 2.5;
            Tgp= Gp * Unit;
            System.out.println("COURSE " + "SCORE " + "GRADE " + "GP ");
            System.out.println(" ");
            System.out.println(Course+" "+Unit+ " " +Score+" "+Grade+" "+Gp+" ");
            System.out.println(" ");

        } 

        else if (Score>=50)
        {
            Grade= "C";
            Gp = 2.0;
            Tgp= Gp * Unit;
            System.out.println("COURSE " + "SCORE " + "GRADE " + "GP ");
            System.out.println(" ");
            System.out.println(Course+" "+Unit+ " " +Score+" "+Grade+" "+Gp+" ");
            System.out.println(" ");

        } 

        else if (Score>=45)
        {
            Grade= "CD";
            Gp = 1.5;
            Tgp= Gp * Unit;
            System.out.println("COURSE " + "SCORE " + "GRADE " + "GP ");
            System.out.println(" ");
            System.out.println(Course+" "+Unit+ " " +Score+" "+Grade+" "+Gp+" ");
            System.out.println(" ");

        } 

        else if (Score>=40)
        {
            Grade= "D";
            Gp = 1.0;
            Tgp= Gp * Unit;
            System.out.println("COURSE " + "SCORE " + "GRADE " + "GP ");
            System.out.println(" ");
            System.out.println(Course+" "+Unit+ " " +Score+" "+Grade+" "+Gp+" ");
            System.out.println(" ");

        } 

        else if (Score>=35)
        {
            Grade= "E";
            Gp = 0.5;
            Tgp= Gp * Unit;
            System.out.println("COURSE " + "SCORE " + "GRADE " + "GP ");
            System.out.println(" ");
            System.out.println(Course+" "+Unit+ " " +Score+" "+Grade+" "+Gp+" ");
            System.out.println(" ");

        } 

        else if (Score>=40)
        {
            Grade= "F";
            Gp = 0.0;
            Tgp= Gp * Unit;
            System.out.println("COURSE " + "SCORE " + "GRADE " + "GP ");

            System.out.println(" ");
            System.out.println(Course+" "+Unit+ " " +Score+" "+Grade+" "+Gp+" ");
            System.out.println(" ");

        } 
        Tunit+=Unit;
        Sgp+=Tgp;

    }
        Gpa= Sgp/Tunit;
        System.out.println(Name);
        System.out.println("F/HD/15/32100"+Mnum);
        System.out.println("THE CGPA OF THE ABOVE NAME STUDENT IS "+ Gpa);
  }

}

1 个答案:

答案 0 :(得分:0)

一个可能的解决方案是创建一个类Student,并使用它们的名称,课程编号,给定课程的编号等创建数据字段。在所示代码之上(无论是IDE,编译器等,你都是'重新使用),它可以开始这样的事情(我想,所以有人纠正我,如果我错了):

Class Student{
   private String name;
   private int matricNum;
   private int courseLoad;

   //then arrange for the getters and setters
   public String getName(){ return name;}
   public int getMatricNum(){ return matricNum;}
   public int getCourseLoad(){ return courseLoad;}


   public void setName(String name){ this.name=name;}
   public void setMatricNum(int matricNum){ this.matricNum = matricNum;}
   public void setCourseLoad(int courseLoad){ this.courseLoad = courseLoad;}

   public Student(String name, int matricNum, int courseLoad){
       this.name = name;
       this.matricNum = matricNum;
       this.courseLoad = courseLoad;
   }

重复此步骤以创建课程类,其中包含课程代码,单元编号和课程分数的数据字段。两者完成后,创建两种类类型的数组:

public Student[] students = new Student[10]; //This creates a student array
 of ten students that will allow you to enter the above data for each student.


public Course[] courses = new Course[(Enter some number here that will accommodate the total number of courses that every student contains)];

我认为对此有效的解决方案是输入初始学生数组的数据,然后输入所述学生的一定数量的课程,然后重复此操作。

然后,在检查成绩时,根据学生的条目迭代Course []数组,然后计算每个班级的成绩。

另一方面,System.out.println();不需要任何空格来执行。它是新行字符“\ n”的较长形式,因此可以写出print语句:

的System.out.println();

无论如何,这个过程可能听起来很乏味,但我认为这对于你想要完成的事情以及效率而言都是足够的。

希望这有帮助!