这是我到目前为止所做的事情。 我不确定如何从这里继续。 这样的问题是否应该使用double for循环?
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);
}
}
}
答案 0 :(得分:0)
首先,您需要列出所有学生的名单。同样,设置一个包含学生属性(姓名,语言,英语,数学)的学生班可能很有用。在获得要处理的学生人数的输入后,您可以循环获取学生数据。从输入中获取学生数据后,请创建一个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
}
答案 1 :(得分:0)
这应该使您入门:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("How many people?");
int input = sc.nextInt(); sc.nextLine();
ArrayList<Student> students = new ArrayList<Student>();
for (int i = 0; i < input; i++) {
System.out.println("Name, Language, English, Math?");
String input2 = sc.nextLine();
String[] myArr = input2.split("[, ]+");
students.add(new Student(myArr[0], tryParse(myArr[1]), tryParse(myArr[2]), tryParse(myArr[3])));
}
for(Student s : students)
System.out.println(s.name);
}
public static double tryParse(String grade) {
return Double.parseDouble(grade);
}
}
class Student{
public String name;
public double language;
public double english;
public double math;
public Student(String name, double language, double english, double math) {
this.name = name;
this.language = language;
this.english = english;
this.math = math;
}
}
尽量不要以小写字母开头类名。这是不好的做法。