|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
需求 按照年龄大小输出
- /*
- * Copyright (c) 2022. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
- * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
- * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
- * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
- * Vestibulum commodo. Ut rhoncus gravida arcu.
- */
- package hapedu.opp;
- public class HomeWork1 {
- public static void main(String[] args) {
- Person[] p = new Person[3];
- p[0] = new Person("jack", "老板", 18);
- p[1] = new Person("mary", "老师", 22);
- p[2] = new Person("aili", "老总", 40);
- for (int i = 0; i < p.length; i++) {
- System.out.println(p[i]);
- }
- //冒泡排序
- Person tmp = null;
- for (int i = 0; i < p.length - 1; i++) { //循环次数
- for (int j = 0; j < p.length - 1 - i; j++) { //比较次数
- //age从大到小排序
- if (p[i].getAge() < p[i + 1].getAge()) {
- tmp = p[i];
- p[i] = p[i + 1];
- p[i + 1] = p[i];
- }
- }
- }
- System.out.println("排序后的结果");
- for (int i = 0; i < p.length; i++) {
- System.out.println(p[i]);
- }
- }
- }
- class Person{
- private String name;
- private String job;
- private int age;
- public Person(String name, String job, int age) {
- this.name = name;
- this.job = job;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getJob() {
- return job;
- }
- public void setJob(String job) {
- this.job = job;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "Person{" +
- "name='" + name + '\'' +
- ", job='" + job + '\'' +
- ", age=" + age +
- '}';
- }
- }
复制代码
不知道为啥运行的不对
- if (p[j].getAge() < p[j + 1].getAge()) {
- tmp = p[j];
- p[j] = p[j + 1];
- p[j + 1] = tmp;
- }
复制代码
|
|