将此代码转换为方法版本

时间:2013-05-08 18:59:11

标签: java methods pass-by-value

我的java项目作业有问题。在这个程序中,当我使用案例1时,它激活学生录音系统。    我需要我必须用方法编写所有这个录音系统。我试过但我失败了。你能告诉我如何编写将数据记录到数组的方法吗?非常感谢。

//===========================================values
String[] name = new String [100];
String[] gender = new String [100];
String[] studentNo = new String [100];
Double[] gpa = new Double [100];
int menu = 0;
int accountNumber = 0;

//===========================================
menu = in.nextInt();
switch (menu) 
case 1:  {
System.out.println("** Recording a new student");
System.out.println("*** Please use lower case");
in.nextLine(); // for solve skipping 

System.out.print("Enter Student Name and Surname: ");
name[accountNumber] = in.nextLine();

System.out.print("Enter Student Gender(m/f): ");
gender[accountNumber] = in.nextLine();

System.out.print("Enter Student Number: ");
studentNo[accountNumber] = in.nextLine();

System.out.print("Enter Student GPA: ");
gpa[accountNumber] = in.nextDouble();

accountNumber++;


System.out.println("New Student Recorded. There are ["+accountNumber+"] students in system.");
System.out.println("");
}break;

1 个答案:

答案 0 :(得分:-1)

嗯,我同意人们做你的功课,使用你必须知道的方法,你必须分开代码,“分而治之”,既然你提交了你的项目,解决方案就可以这样(使用数组)当然),我会使用bean或dto来存储信息,但可能需要你使用数组:

public class DoYourHomework {

    String[] name = new String[100];
    String[] gender = new String[100];
    String[] studentNo = new String[100];
    Double[] gpa = new Double[100];
    int menu = 0;
    int accountNumber = 0;

    public DoYourHomework() {
        Scanner in = new Scanner(System.in);
        menu = in.nextInt();
        switch (menu) {
        case 1:  {
        System.out.println("** Recording a new student");
        System.out.println("*** Please use lower case");
        in.nextLine(); // for solve skipping 

        String name = null;
        String number = null;
        String gender = null;
        double gpa = -1;

        System.out.print("Enter Student Name and Surname: ");
        name = in.nextLine();

        System.out.print("Enter Student Gender(m/f): ");
        gender = in.nextLine();

        System.out.print("Enter Student Number: ");
        number = in.nextLine();

        System.out.print("Enter Student GPA: ");
        gpa = in.nextDouble();

        registerNewStudent(name, gender, number, gpa);
        accountNumber++;


        System.out.println("New Student Recorded. There are ["+accountNumber+"] students in system.");
        System.out.println("");
        }
        }

    }

    private void registerNewStudent(String name2, String gender2,
            String number, double gpa2) {
        name[accountNumber] = name2;
        gender[accountNumber] = gender2;
        studentNo[accountNumber] = number;
        gpa[accountNumber] = gpa2;
    }

    public static void main(String[] args) {
        new DoYourHomework();
    }

}

希望它可以帮助您理解下一个练习。也许,作为一个加号,你可以添加验证,以避免相同的学号或尝试自己做“修改和删除”等下一步操作。最好的问候。