你怎么写方法定义?

时间:2018-01-21 19:23:43

标签: java methods

这是我的方法签名,我正在编写这个非常长的代码,它应该更新学生的标记,并根据学生编号和测试编号以及yada yada yada更新成绩簿表中的信息。无论如何,在所有这些之前,我将不得不为方法编写方法定义,有没有人知道如何做到这一点的一般格式?到目前为止,我的代码顺便说一下:

import java.util.Scanner;
public class GradeBook {
    private int numberofStudents;
    private int numberofTests;
    private int studId;

    private int [] [] table;

    private GradeBook()
    {
        Scanner keyboard = new Scanner (System.in);

        System.out.println("Enter the number of student in the class");

        int numberofStudents = keyboard.nextInt();

        System.out.println("Enter the number of tests taken for the term");
        int numberOfTests = keyboard.nextInt();

        table = new int [numberofStudents][numberofTests];
        System.out.println("Enter the students marks by ID number");

        System.out.println();
        for(int studId = 1; studId <= numberofStudents; studId++) {

                System.out.println('\n' + "Student ID: " + studId);
                for(int testNo =1; testNo <= numberOfTests; testNo++) {
                        System.out.println(" Test Number: " + testNo + '\t');

                        table [studId -1] [testNo - 1] = keyboard.nextInt();
        }
    }
}
    private void display()
    {
        int row, column;
        for(row =0; row<table.length; row++);
        {    
            System.out.print("Student ID: " + (row +1) + "Tests: ");

            for(column = 0; column < table[row]. length; column++)
                System.out.print(table[row] [column] + " ");

            System.out.println();
        }    
   }

    public static void main(String[] args) {
        GradeBook myBook = new GradeBook();
        myBook.display();
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:0)

首先,您显示类定义,而不是方法签名。

您可能希望简化该构造函数,以免在那里做太多工作。只需初始化一个表。

public class GradeBook {

    private int[][] table;

    public GradeBook(int students, int tests) {
        this.table = new int[students];
        for (int i = 0; i < students; i++) {
            this.table[i] = new int[tests];
        } 
        loadGrades();
    }

以下是一些“方法定义”的示例。

    public void loadGrades() {
        Scanner keyboard = new Scanner(System.in);
        // TODO: load the test scores
    }

    public void getNumberOfStudents() {
        return this.table.length;
    }

    pubilc void display() {
        // TODO: Display all tests from all students
    }

    public void display(int studentId) {
       System.out.println(Arrays.toString(getTests(studentId));
    }

    public void setTest(int studentId, int testId, int grade) {
        this.table[studentId][testId] = grade;
    }

    public int[] getTests(int studentId) {
        return this.table[studentId];
    }

我认为你应该将提示移动到main方法,而不是对象构造函数。

    public static void main(String[] args) {

        Scanner keyboard = new Scanner (System.in);

        System.out.println("Enter the number of student in the class");
        int numberofStudents = keyboard.nextInt();

        System.out.println("Enter the number of tests taken for the term");
        int numberOfTests = keyboard.nextInt();

        GradeBook g = new GradeBook(numberofStudents, numberOfTests);
        g.display();
    } 
}
相关问题