首页 > 解决方案 > 存储姓名、语言标记、英文标记和数学标记。取三个分数的平均值。我不确定如何继续

问题描述

这就是我到目前为止所做的。我不确定如何从这里继续。我应该使用双循环来解决这样的问题吗?

public class testing {

    public static void main(String[] args) throws IOException{
        Scanner sc = new Scanner(System.in);
        System.out.println("How many people?");
        String input = sc.nextLine();
        int z = Integer.parseInt(input);
        for(int i =0; i <=z; i++) {
            System.out.println("Name,Language,English,Math?");
            String input2 = sc.nextLine();
            String[] myArr = new String[] {input2};

            for(int j = 0; j<4; j++) {
                String [] myArr1 = myArr[j].split(",");
                System.out.println(myArr1[0]);
            }
            //System.out.println(myArr[0]);
            //student student1 = new student(myArr[i]);
            for(int j = 0; j< 4; j++) {
                String[] studentpl = myArr[i].split(",");
            }
            //ArrayList<student> aList = new ArrayList<student>();
            //aList.add(input2);
            //student1 student new student1();
            //student stu = new student(input);
        }
    }
}

标签: java

解决方案


首先,您需要制作一个包含所有学生的列表。此外,创建一个包含学生属性(姓名、语言、英语、数学)的学生类也可能很有用。在获得要处理的学生数量的输入后,您可以循环获取学生数据。从输入中获取学生数据后,创建一个 Student 类的实例并将所有这些获取的数据设置为该类。设置完所有数据后,您就可以将学生添加到您的学生列表中。我在下面包含了一个示例代码,但此代码在输入中没有错误检查。例如,您可以检查 numberOfStudents 的输入是否有效。这段代码可以改进,但为了简单起见,我忽略了这些检查。

这是主要课程

public class Testing {
    public static void main(String[] args) throws IOException{
        Scanner sc = new Scanner(System.in);
        System.out.println("How many people?");
        int numberOfStudents = sc.nextInt();
        ArrayList<Student> studentList = new ArrayList<Student>();

        for(int i = 0; i < numberOfStudents; i++) {
            System.out.println("Name,Language,English,Math?");
            String dataInput = sc.nextLine();
            String[] dataInput = dataInput.split(",");
            // You can add here checking if the dataInput is valid. Example if it really contains all the needed input by checking the size of dataInput

            Student student = new Student();
            student.setName(dataInput[0]);
            student.setLanguage(dataInput[1]);
            student.setEnglish(dataInput[2]);
            student.setMath(dataInput[3]);
            studentList.add(student);
        }
    }
}

这里是学生课。您应该将此类导入到您的主类中。

public class Student {
     private name;
     private language;
     private english;
     private math;

     // insert here getter and setter methods for each attribute
}

推荐阅读