我正在尝试用java构建一个具有多种数据类型的数组
我是java编程的初学者,我有一个问题需要我输入:学生姓名、id、数学成绩、英语成绩和科学成绩,然后计算他的总成绩和平均成绩。所以我试图把它变成一个2d数组,但我不知道如何把字符串数组变成int。有没有办法让一个数组有多种数据类型(char、int、string等)? 这是到目前为止我的密码我正在尝试用java构建一个具有多种数据类型的数组,java,arrays,Java,Arrays,我是java编程的初学者,我有一个问题需要我输入:学生姓名、id、数学成绩、英语成绩和科学成绩,然后计算他的总成绩和平均成绩。所以我试图把它变成一个2d数组,但我不知道如何把字符串数组变成int。有没有办法让一个数组有多种数据类型(char、int、string等)? 这是到目前为止我的密码 int columns = input.nextInt(); int rows=5; int StudentName=0; int StudentID=1;
int columns = input.nextInt();
int rows=5;
int StudentName=0;
int StudentID=1;
int math=2;
int English=3;
int Science = 4;
int i = 0;
String[][] newArray = new String[columns][rows];
for (i = 0; i<columns;i++){
System.out.println("Enter Student Name ");
newArray[i][StudentName] = input.nextLine();
System.out.println("Enter Student ID ");
newArray[i][StudentID] = input.nextLine();
System.out.println("Enter Student Math Score ");
newArray[i][math] = input.nextLine();
System.out.println("Enter Student English Score ");
newArray[i][English] = input.nextLine();
System.out.println("Enter Student Science Score ");
newArray[i][Science] = input.nextLine();
}
for(i = 0; i < columns; i++){
for(int j = 0; j < 5; j++){
System.out.println(newArray[i][j]);
}
}
}
int columns=input.nextInt();
int行=5;
int StudentName=0;
int StudentID=1;
int-math=2;
国际英语=3;
int-Science=4;
int i=0;
字符串[][]新数组=新字符串[列][行];
对于(i=0;i您可以使用对象数组。因为最终,Java中的任何东西都是对象(或具有“对象同级”-类似于原始int值的整数类)。因此,可以使用对象数组存储组成瞳孔的各种不同元素
但这是错误的方法。您使用类和对象对代码必须处理的实体进行建模
因此,创建一个代表学生的类。该类具有诸如姓名、id、成绩等字段。然后,最终创建一个Student
的一维数组!这就是OO编程的本质:创建有用的抽象,而不是通过将数据填充到数组和内存中来耦合数据记住哪个索引“关联”了属于一起的事物。相反:使这种关系明确化!1.创建一个域对象,它将成为您的数据结构,并且是不可变的
例如:
公共类StudentInfo实现可序列化{
private static final long serialVersionUID = 1L;
private final String StudentName;
private final int StudentID;
public StudentInfo (String StudentName, int StudentID) {
this.StudentName= StudentName;
this.StudentID= StudentID;
}
public String getStudentName() {
return StudentName;
}
//更多访问方法
一个生成器方法以增量方式创建对象
public class StudentInfoBuilder {
private String StudentName;
private int StudentID;
private StudentInfo() {
}
public static JobInfoBuilder newBuilder() {
return new JobInfoBuilder();
}
public JobInfoBuilder withJobName(String StudentName) {
this.StudentName = StudentName;
return this;
}
public JobInfo build() {
return new StudentInfo(StudentName, StudentID);
}
}
为了简单明了,您可以执行以下步骤:
创建具有所有必需文件(具有必需类型)的学生
模型:
定义从控制台读取所有学员的方法:
public static Student[] readStudents(Scanner input) {
Student[] students = new Student[input.nextInt()];
for (int i = 0; i < students.length; i++) {
Student student = new Student();
students[i] = student;
System.out.println("Enter Student Name ");
student.setName(input.nextLine());
System.out.println("Enter Student ID ");
student.setId(input.nextLine());
System.out.println("Enter Student Math Score ");
student.setMathScore(input.nextLine());
System.out.println("Enter Student English Score ");
student.setEnglishScore(input.nextLine());
System.out.println("Enter Student Science Score ");
student.setScienceScore(input.nextLine());
}
return students;
}
publicstaticstudent[]readStudents(扫描仪输入){
学生[]学生=新学生[input.nextInt()];
for(int i=0;i
定义将所有学员打印到控制台的方法:
public static void print(Student[] students) {
for (int i = 0; i < students.length; i++) {
Student student = students[i];
System.out.printf("Student Name: %s", student.getName());
System.out.printf("Student ID: %s", student.getId());
System.out.printf("Student Math Score: %s", student.getMathScore());
System.out.printf("Student English Score: %s", student.getEnglishScore());
System.out.printf("Student Science Score: %s", student.getScienceScore());
System.out.println();
}
}
公共静态无效打印(学生[]学生){
for(int i=0;i
p.S.您可以使用array
或list
作为集合。一般来说:如果您不打算修改集合的大小,请使用array
。可以。下面是示例代码。您可以按照相同的过程尝试计算总和和平均值
class Student{
String name;
int mark1;
int mark2;
Student(String name,int mark1,int mark2){
this.name=name;
this.mark1=mark1;
this.mark2=mark2;
}
}
public class DiffDataType {
public static void main(String[] args) {
//Creating user defined class objects
Student s1=new Student("Mark",90,75);
Student s2=new Student("John",74,65);
Student s3=new Student("Simon",65,86);
ArrayList<Student> al=new ArrayList<Student>();
al.add(s1);
al.add(s2);
al.add(s3);
Iterator itr=al.iterator();
//traverse elements of ArrayList object
while(itr.hasNext()){
Student st=(Student)itr.next();
System.out.println(st.name+" "+st.mark1+""+st.mark2);
}
}
}
班级学生{
字符串名;
int-mark1;
int-mark2;
学生(字符串名,int-mark1,int-mark2){
this.name=name;
这个.mark1=mark1;
这个.mark2=mark2;
}
}
公共类DiffDataType{
公共静态void main(字符串[]args){
//创建用户定义的类对象
学生s1=新生(“分数”,90,75);
学生s2=新生(“约翰”,74,65);
学生s3=新生(“西蒙”,65,86);
ArrayList al=新的ArrayList();
al.添加(s1);
al.添加(s2);
al.add(s3);
迭代器itr=al.Iterator();
//遍历ArrayList对象的元素
而(itr.hasNext()){
学生st=(学生)itr.next();
System.out.println(st.name+“”+st.mark1+“”+st.mark2);
}
}
}
希望这能回答您的问题。习惯于使用几个其他数据结构和java语句:
class Student {
String name;
String id;
//int mathScore;
//int englishScore;
//int scienceScore;
int[] scores = new int[3];
}
由于数组无法增长,请使用作为ArrayList实现的列表
List<Student> students = new ArrayList<>();
while (input.hasNextLine()) {
System.out.println("Enter Student Name ");
String line = input.nextLine();
if (line.equals("end")) {
break;
}
// Create a new student:
Student student = new Student();
student.name = line;
System.out.println("Enter Student ID ");
student.id = input.nextLine();
System.out.println("Enter Student Math Score ");
String line = input.nextLine();
student.scores[0] = Integer.parseInt(line);
System.out.println("Enter Student English Score ");
line = input.nextLine();
student.scores[1] = Integer.parseInt(line);
System.out.println("Enter Student Science Score ");
line = input.nextLine();
student.scores[2] = Integer.parseInt(line);
// Add the student to the list:
students.add(student);
}
// One way to walk through the list:
for (Student student : students) {}
System.out.printf("Name %s, ID %s, Math: %d.%n",
student.name, student.id, student.scores[0]);
}
List students=new ArrayList();
while(input.hasNextLine()){
System.out.println(“输入学生姓名”);
String line=input.nextLine();
如果(行等于(“结束”)){
打破
}
//创建新学员:
学生=新生();
student.name=行;
System.out.println(“输入学生ID”);
student.id=input.nextLine();
System.out.println(“输入学生数学分数”);
String line=input.nextLine();
student.scores[0]=整数.parseInt(行);
System.out.println(“输入学生英语成绩”);
line=input.nextLine();
student.scores[1]=整数.parseInt(行);
System.out.println(“输入学生科学分数”);
line=input.nextLine();
student.scores[2]=整数.parseInt(行);
//将学生添加到列表中:
学生。添加(学生);
}
//浏览列表的一种方法:
(学生:学生){}
System.out.printf(“名称%s,ID%s,数学:%d.%n”,
List<Student> students = new ArrayList<>();
while (input.hasNextLine()) {
System.out.println("Enter Student Name ");
String line = input.nextLine();
if (line.equals("end")) {
break;
}
// Create a new student:
Student student = new Student();
student.name = line;
System.out.println("Enter Student ID ");
student.id = input.nextLine();
System.out.println("Enter Student Math Score ");
String line = input.nextLine();
student.scores[0] = Integer.parseInt(line);
System.out.println("Enter Student English Score ");
line = input.nextLine();
student.scores[1] = Integer.parseInt(line);
System.out.println("Enter Student Science Score ");
line = input.nextLine();
student.scores[2] = Integer.parseInt(line);
// Add the student to the list:
students.add(student);
}
// One way to walk through the list:
for (Student student : students) {}
System.out.printf("Name %s, ID %s, Math: %d.%n",
student.name, student.id, student.scores[0]);
}
Map<Integer, Student> students = new HashMap<>();
for (int i = 0; i < students.length; i++) {
Student student = new Student("Mark", 90, 75);
map.put(i, student);
}