-3

我的 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;
4

1 回答 1

-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();
    }

}

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

于 2013-05-08T19:52:40.667 回答