Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何编写方法定义?_Java_Methods - Fatal编程技术网

Java 如何编写方法定义?

Java 如何编写方法定义?,java,methods,Java,Methods,这是我的方法签名,我正在写一个很长的代码,它应该更新学生的分数,更新成绩表中的信息,给定一个学生编号、考试编号和yada yada yada。无论如何,在所有这些之前,我要为一个方法写一个方法定义,有人知道如何做的通用格式吗?顺便说一下,这是我的代码: import java.util.Scanner; public class GradeBook { private int numberofStudents; private int numberofTests; pri

这是我的方法签名,我正在写一个很长的代码,它应该更新学生的分数,更新成绩表中的信息,给定一个学生编号、考试编号和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

    }

}

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

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

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();
    }
我认为应该将提示移到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();
    } 
}

成绩册是一个建设者,不是一个方法。。。你确实有一个方法显示,所以我不明白你需要什么。不,我知道它是一个构造函数,我想知道如何编写一个你已经做过的方法定义。官方文件中的私人文件:以及更详尽的解释:
    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();
    } 
}