Java代码,求大神帮忙

2024-12-03 05:57:26
推荐回答(2个)
回答1:

不会,楼下回答

回答2:

import java.util.ArrayList;

public class test

{

public static void main(String[] args) {

ArrayList allstudents = new ArrayList();

String str = "01张三23|02李四30|03王五21|04赵六17|05田七19|06腊八19";

String[] studentstring = str.split("\\|");

System.out.println(studentstring.length);

for (int i = 0; i < studentstring.length; i++) {

Student student = new Student();

student.setID(Integer.parseInt(studentstring[i].substring(0, 2)));

student.setName(studentstring[i].substring(2, 4));

student.setAge(Integer.parseInt(studentstring[i].substring(4, 6)));

allstudents.add(student);

}

System.out.println("元素个数" + allstudents.size());

int maxage = 0;

int sumage = 0;

for (int i = 0; i < allstudents.size(); i++) {

if(allstudents.get(i).getID() == 5)

{

System.out.println("ID 5 is :" + allstudents.get(i));

}

if(allstudents.get(i).getID() == 2)

{

allstudents.get(i).setAge(allstudents.get(i).getAge()+1);

}

if(allstudents.get(i).getAge() >= 20&&allstudents.get(i).getAge() <= 30)

{

System.out.println("age in 20-30 : " + allstudents.get(i));

}

if(allstudents.get(i).getAge()>maxage)

{

maxage = allstudents.get(i).getAge();

}

sumage += allstudents.get(i).getAge();

}

System.out.println("maxage:" + maxage + "sumage" + sumage);

System.out.println(allstudents);

}

}

class Student

{

private int ID;

private String Name;

private int Age;

public int getID() {

return ID;

}

public void setID(int iD) {

ID = iD;

}

public String getName() {

return Name;

}

public void setName(String name) {

Name = name;

}

public int getAge() {

return Age;

}

public void setAge(int age) {

Age = age;

}

@Override

public String toString() {

return "Student [ID=" + ID + ", Name=" + Name + ", Age=" + Age + "]";

}

}