如题,本文为笔者的笔记 希望能帮到你
package com.mxy.homework;
import java.util.Scanner;
/**
* @author MXHstrat
* @create 2021 - 11 - 02 19:05
*/
public class CustomerTest02 {
public static void main(String[] args) {
CustomerHandle c1 = new CustomerHandle();
do {
Customer02 cust02 = new Customer02();
System.out.println("添加客户的姓名:");
cust02.name = new Scanner(System.in).next();
System.out.println("添加客户的年龄:");
cust02.age = new Scanner(System.in).nextInt();
System.out.println("添加客户的积分:");
cust02.integral = new Scanner(System.in).nextInt();
c1.add(cust02);
System.out.println("是否继续再添加一个客户信息(y/n)");
} while ("Y".equalsIgnoreCase(new Scanner(System.in).next()));
c1.showAll();
System.out.println("按积分进行升序排列为:");
c1.ascSort();
System.out.println("按积分进行降序排列为:");
c1.descSort();
}
}
class CustomerHandle {
Customer02[] cust = new Customer02[1000];
public void add(Customer02 customer02s) {
for (int i = 0; i < cust.length; i++) {
if (cust[i] == null) {
cust[i] = customer02s;
break;
}
}
}
public void showAll() {
System.out.println("名字\t" + "\t年龄\t" + "\t积分");
System.out.println("-----------------------------");
for (Customer02 abc : cust) {
if (abc != null) {
abc.getShow();
}
}
System.out.println("-----------------------------");
}
public void ascSort() {
for (int i = 0; i < cust.length; i++) {
for (int j = 0; j < cust.length - 1 - i; j++) {
if (cust[j+1] != null) {
if (cust[j].integral > cust[j + 1].integral) {
Customer02 temp = cust[j];
cust[j] = cust[j + 1];
cust[j + 1] = temp;
}
}
}
}
System.out.println("名字\t" + "\t年龄\t" + "\t积分");
System.out.println("-----------------------------");
for (Customer02 abc : cust) {
if (abc != null) {
abc.getShow();
}
}
System.out.println("-----------------------------");
}
public void descSort() {
for (int i = 0; i < cust.length; i++) {
for (int j = 0; j < cust.length - 1 - i; j++) {
if (cust[j+1] != null) {
if (cust[j].integral < cust[j + 1].integral) {
Customer02 temp = cust[j];
cust[j] = cust[j + 1];
cust[j + 1] = temp;
}
}
}
}
System.out.println("名字\t" + "\t年龄\t" + "\t积分");
System.out.println("-----------------------------");
for (Customer02 abc : cust) {
if (abc != null) {
abc.getShow();
}
}
System.out.println("-----------------------------");
}
}
------------------------------------------------------分割线------------------------------------------------------
package com.mxy.homework;
/**
* @author MXHstrat
* @create 2021 - 11 - 02 19:07
*/
public class Customer02 {
String name;
int age;
int integral;
public void getShow(){
System.out.println(name +"\t"+age+"\t"+"\t"+integral);
}
}