8.11
(1)学生管理系统菜单搭建
代码示例:
import java.util.Scanner;
public class StudentSystem {
public static void main(String[] args) {
loop: while(true) {
System.out.println("--------欢迎进入学生管理系统---------");
System.out.println("1:添加学生");
System.out.println("2:删除学生");
System.out.println("3:修改学生");
System.out.println("4:查询学生");
System.out.println("5:退出");
System.out.println("请输入您的选择:");
Scanner sc =new Scanner(System.in);
String choose=sc.next();
switch (choose) {
case "1" -> addStudent() ;
case "2" -> deleteStudent();
case "3" -> updateStudent();
case "4" -> queryStudent();
case "5" -> {
System.out.println("退出");
break loop;
}
default -> System.out.println("没有这个选项");
}
}
}
public static void addStudent(){
System.out.println("添加学生");
}
public static void deleteStudent(){
System.out.println("删除学生");
}
public static void updateStudent(){
System.out.println("修改学生");
}
public static void queryStudent(){
System.out.println("查询学生");
}
}
javabean:public class Student {
private String id;
private String name;
private int age;
private String address;
public Student(){
}
public Student(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
8.12
(1)学生管理系统(添加功能)
方法代码:
public static void addStudent(ArrayList
Student s=new Student();
Scanner sc=new Scanner(System.in);
String id=null;
while (true) {
System.out.println("请输入学生id");
id = sc.next();
Boolean flag=contains(list, id);
if(flag) {
System.out.println("该id已经存在,请重新输入");
}else {
s.setId(id);
break;
}
}
System.out.println("请输入学生姓名");
String name=sc.next();
s.setName(name);
System.out.println("请输入学生年龄");
int age=sc.nextInt();
s.setAge(age);
System.out.println("请输入学生家庭住址");
String address=sc.next();
s.setAddress(address);
list.add(s);
System.out.println("添加成功");
}
public static boolean contains(ArrayList
for(int i=0;i<list.size();i++) {
Student student=list.get(i);
String sid=student.getId();
if(sid.equals(id)) {
return true;
}
}
return false;
}
(2)删除功能
方法代码:
public static void deleteStudent(ArrayList
System.out.println("请输入要删除的学生id");
Scanner sc=new Scanner(System.in);
String id=sc.next();
int index=getIndex(list, id);
if(index>=0) {
list.remove(index);
System.out.println("删除成功");
}else {
System.out.println("id不存在");
}
}
public static int getIndex(ArrayList
for (int i = 0;i<list.size() ; i++) {
Student stu=list.get(i); //得到学生对象
String sid=stu.getId(); //得到id
if(sid.equals(id)) { //进行比较
return i;
}
}
return -1;
}
8.13
(1)学生管理系统(修改学生)
方法代码:
public static void updateStudent(ArrayList
System.out.println("请输入要修改学生的id");
Scanner sc=new Scanner(System.in);
String id=sc.next();
int index=getIndex(list, id);
if(index==-1) {
System.out.println("要修改的id"+id+"不存在,请重新输入");
return;
}
Student stu=list.get(index);
System.out.println("请输入学生姓名");
String newname=sc.next();
stu.setName(newname);
System.out.println("请输入学生年龄");
int newage=sc.nextInt();
stu.setAge(newage);
System.out.println("请输入学生地址");
String newaddress=sc.next();
stu.setAddress(newaddress);
System.out.println("修改成功");
}
(2)查询功能
方法代码:
public static void queryStudent(ArrayList
if(list.size()0) {
System.out.println("当前无学生信息");
return;
}
System.out.println("id\t\t姓名\t年龄\t家庭住址");
for (int i = 0; i < list.size(); i++) {
Student stu=list.get(i);
System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
}
}
8.14
(1)查找算法:基本查找
代码示例:
import java.util.Scanner;
public class Suanfa {
public static void main(String[] args) {
int[] arr= {114,514,1919,810};
System.out.println("请输入要查找的数");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
System.out.println(search(arr, num));
}
public static boolean search(int[] arr,int num) {
for (int i = 0; i < arr.length; i++) {
if(arr[i]num) {
return true;
}
}
return false;
}
}
(2)二分(折半)查找
前提:数组中的数据必须是有序的
代码示例:
//查找数在第几个
import java.util.Scanner;
public class Zheban {
public static void main(String[] args) {
int[] arr= {1,4,5,8,9,11};
System.out.println("请输入要查找的数");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
System.out.println(search(arr, num)+1);
}
public static int search(int[] arr,int num) {
int min=0;
int max=arr.length-1;
while(true) {
if(max<min) {
return -1;
}
int mid=(min+max)/2;
if(arr[mid]>num) {
max=mid-1;
}else if(arr[mid]<num){
min=min+1;
}else {
return mid;
}
}
}
}
8.15
(1)冒泡排序
代码示例:
public class Maopao {
public static void main(String[] args) {
int[] arr= {11,45,14,191,9,810};
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if(arr[j]>arr[j+1]) {
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
8.16
(1)Arrays
操作数组的工具类
方法:
public static String toString(数组) 把数组拼接成一个字符串
public static int binarySearch(数组,查找的元素) 二分查找元素
public static int[] copyOf(原数组,新数组长度) 拷贝数组
public static int[] copyOfRange(原数组,起始索引,结束索引) 拷贝数组(指定范围)
public static void fill(数组,元素) 填充数组
public static void sort(数组) 按照默认方式进行数组排序
public static void sort(数组,排序规则) 按照指定规则排序
注:二分查找时,如果查找的元素是存在的,返回值是真实的索引,如果是不存在的,返回是负插入点-1
代码示例:
8.17
(1)五位随机验证码
代码示例:
import java.util.Random;
public class Test {
public static void main(String[] args) {
char[] chs=new char[52];
for (int i = 0; i < chs.length; i++) {
if(i<=25) {
chs[i]=(char)(97+i);
}else {
chs[i]=(char)(65+i-26);
}
}
String result="";
Random r=new Random();
for (int i = 0; i < 4; i++) {
int randomIndex=r.nextInt(chs.length);
result=result+chs[randomIndex];
}
int number=r.nextInt(10);
result=result+number;
System.out.println(result);
}
}