实现方法:编写两个类,一个用main,另一个用not

时间:2015-12-12 05:24:49

标签: java class

我想要写一个包含以下内容的课程。

  • 名字

  • 姓氏

  • ID

  • 得分1

  • Score2

  • Score3

  • 平均

  • 成绩(A,A-,B +,...... E)。

所有数据成员都被认为是私有的。

以下是我所拥有的,我将该课程命名为学生。

import java.util.Scanner;

public class Student {
       private String fname;
        private String lname;
        private int id;
        private int score1;
        private int score2;
        private int score3;
        private double average;
        private String grade;

        public void readInfo()
        {
            Scanner k = null;
            k = new Scanner (System.in);

            System.out.println ("Please enter the first name: (Enter John as first name and Doe as last name to stop) ");
            fname = k.next();
            System.out.println("Please enter the last name: ");
            lname = k.next();
            if (fname.equalsIgnoreCase("John")&&lname.equalsIgnoreCase("Doe"))
            System.exit(0);
            System.out.println("Please enter the student ID: ");
            id = k.nextInt();
            System.out.println("Please enter the first score: ");
            score1 = k.nextInt();
            System.out.println("Please enter the second score: ");
            score2 = k.nextInt();
            System.out.println("Please enter the third score: ");
            score3 = k.nextInt();
        }

        private void computeAverage()
        {
            average = (score1+score2+score3)/3;

        }
        private void computeGrade()
        {
            if (average>=90&&average<=100)
                grade = "A";
            else if (average>=80&&average<=89.9)
                grade = "B";
            else if (average>=70&&average<=79.9)
                grade = "C";
            else if (average>=60&&average<=69.9)
                grade = "D";
            else 
                grade = "F";
        }

        private String getName()
        {
            return fname + " " + lname;
        }
        private double getAverage()
        {
            return average;
        }
        public void printAll()
        {
            System.out.printf("%-9s%-10s %-9s %-3s %-3s %-3s %-5s  %-5s", "LastName","FirstName","ID","S1","S2","S3","AVG","GRADE");
            System.out.println();
            System.out.println("-----------------------------------------------------");
            System.out.printf("%-9s%-9s  %-9d %-3d %-3d %-3d %-3.2f%-2s",lname,fname,id,score1,score2,score3,average,grade);
            System.out.println();
            System.out.println("-----------------------------------------------------");

我遇到的问题是如何实际获取要在控制台上显示的信息。我继续创建了一个实例,并将其称为Student类的Averages(上面列出的类。)

以下是我对其他名为&#39; Averages&#39;的课程的看法。我迷失了该怎么做。实际上第一次在Java中,只研究了两个星期。我的书没有澄清任何内容。所以?我假设我仍在学习绳索并感到恼火,我无法想象这可能(简单)部分。感谢。

import java.util.Scanner;

public class Averages {

    public static void main(String[] args) {
        Scanner k= new Scanner(System.in);

        Student[] students = new Student[36];

        Student st, st2;
        st = new Student();
        st2 = new Student();





        System.out.printf("%-9s%-10s %-9s %-3s %-3s %-3s %-5s  %-5s", "LastName","FirstName","ID","S1","S2","S3","AVG","GRADE");
        System.out.println();
        System.out.println("-----------------------------------------------------");
        //System.out.printf("%-9s%-9s  %-9d %-3d %-3d %-3d %-3.2f%-2s",lname,fname,id,score1,score2,score3,average,grade);
        System.out.println();
        System.out.println("-----------------------------------------------------");

1 个答案:

答案 0 :(得分:0)

这是一段可以帮助您的代码。

它主要是与学生一起填充阵列。数据,并计算和打印班级平均值:

public static void main(String[] args) {

int classSize = 36;
double cumulatedAverages = 0d;

Student[] students = new Student[classSize];

 for(int i =0;i<students.length;i++){
    Student student = new Student();
    // populate student's data
    student.readInfo();
    // make calculations
    student.computeGrade();
    student.computeAverage();
    cumulatedAverages  = cumulatedAverages + student.getAverage();
    // add the student to the array
    students[i] = student;
}

// compute the class average
double classAverage = cumulatedAverages / classSize;

System.out.printf("%s %-3.2f","Class average is",classAverage);

}