- package cn.itcast.studentpack;
-
- public class StudentMethod {
-
- private long sno;
- private String name;
- private int age;
- private String address;
-
- public StudentMethod() {
- }
-
- public StudentMethod(long sno, String name, int age, String address) {
- this.sno = sno;
- this.name = name;
- this.age = age;
- this.address = address;
- }
-
- public long getSno() {
- return sno;
- }
-
- public void setSno(long sno) {
- this.sno = sno;
- }
-
- 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;
- }
- }
- /**
- * @author LHB 学生管理系统 编写一个模拟学生管理系统程序,可以实现对学生信息的添加、删除、修改和查询功能,
- * 要求使用List集合存储自定义的对象集合,并熟练使用List集合的中常用方法实现相关的操作。
- */
- package cn.itcast.studentpack;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
-
- public class StudentTest {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
-
- List
list = new ArrayList<>(); -
- while (true) {
- printInfo();
- switch (sc.nextInt()) {
- case 1:
- showStudent(list);
- break;
- case 2:
- addStudent(list);
- break;
- case 3:
- deleteStudent(list);
- break;
- case 4:
- updateStudent(list);
- System.out.println("修改学生");
- break;
- case 5:
- System.out.println("退出系统");
- System.exit(0);
- break;
- default:
- System.out.println("输入有误,请重新输入!");
- break;
- }
- }
- }
-
- public static void printInfo() {
- 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("请输入你的选择:");
- }
-
- private static void addStudent(List
list) { - StudentMethod s = new StudentMethod();
- Scanner sc = new Scanner(System.in);
- long sno;
- while (true) {
- System.out.print("学号:");
- sno = sc.nextLong();
- boolean flag = false;
- for (StudentMethod stu : list) {
- if (sno == stu.getSno()) {
- flag = true;
- break;
- }
- }
- if (flag) {
- System.out.println("学号重复了!请重新输入:");
- } else {
-
- System.out.print("姓名:");
- String name = sc.next();
- System.out.print("年龄:");
- int age = sc.nextInt();
- System.out.print("地址:");
- String address = sc.next();
-
- s.setSno(sno);
- s.setName(name);
- s.setAge(age);
- s.setAddress(address);
- list.add(s);
- System.out.println("添加完成!\n");
- break;
- }
- }
- }
-
- private static void deleteStudent(List
list) { - Scanner sc = new Scanner(System.in);
- System.out.println("请输入要删除的学生的学号:");
- long sno = sc.nextLong();
- int idx = -1;
- for (int i = 0; i < list.size(); i++) {
- StudentMethod stu = list.get(i);
- if (sno == stu.getSno()) {
- idx = i;
- break;
- }
- }
- if (idx != -1) {
- System.out.println("删除成功!\n");
- list.remove(idx);
- } else {
- System.out.println("没有此位同学的信息!请重新选择!");
- }
- }
-
- private static void updateStudent(List
list) { - Scanner sc = new Scanner(System.in);
- System.out.println("请输入要修改的学生的学号:");
- long sno = sc.nextLong();
- int idx = -1;
- for (int i = 0; i < list.size(); i++) {
- StudentMethod s = list.get(i);
- if (s.getSno() == sno) {
- idx = i;
- break;
- }
- }
- if (idx != -1) {
- StudentMethod s = new StudentMethod();
- 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.set(idx, s);
- System.out.println("修改完成!\n");
- } else {
- System.out.println("没有此同学的信息,请重新操作!\n");
- }
-
- }
-
- private static void showStudent(List
list) { - boolean empty = isEmpty(list);
- if (empty) {
- System.out.println("不好意思,目前没有学生信息可供查询,请回去重新选择你的操作!\n");
- return;
- } else {
- for (StudentMethod student : list) {
- System.out.println(student.getSno() + ", " + student.getName() + ", " + student.getAge() + ", "
- + student.getAddress());
- }
- System.out.println();
- }
- }
-
- private static boolean isEmpty(List
list) { - if (list.isEmpty())
- return true;
- else
- return false;
- }
- }
编写一个斗地主的洗牌发牌程序,按照斗地主的规则完成洗牌发牌的过程。一副扑克总共有54张牌,牌面由花色和数字组成(包括J、Q、K、A字母)组成,花色有♠、♥、♦、♣ 四种,分别表示黑桃、红桃、方块、梅花,小☺、大☻分别表示小王和大王。斗地主游戏共有三位玩家参与,首先将这54张牌的顺序打乱每人轮流摸一次牌,剩余3张留作底牌,然后在控制台打印三位玩家的牌和三张底牌。
- /**
- * @author LHB 斗地主
- *
- */
- package cn.cards;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
-
- public class playcards {
- public static void main(String[] args) {
- // 花色
- ArrayList
color = new ArrayList(); - color.add("♠");
- color.add("♥");
- color.add("♦");
- color.add("♣");
- // 排序
- ArrayList
number = new ArrayList(); - for (int i = 3; i <= 10; i++) {
- number.add(i + "");
- }
- number.add("J");
- number.add("Q");
- number.add("K");
- number.add("A");
- number.add("2");
- HashMap
map = new HashMap(); - int index = 0;
- for (String thisNumber : number) {
- for (String thisColor : color) {
- map.put(index++, thisColor + thisNumber);
- }
- }
- // 大小王
- map.put(index++, "小☺");
- map.put(index++, "大☻");
- ArrayList
cards = new ArrayList(); - for (int i = 0; i <= 53; i++) {
- cards.add(i);
- }
- Collections.shuffle(cards);
- ArrayList
iPlayer = new ArrayList(); - ArrayList
iPlayer2 = new ArrayList(); - ArrayList
iPlayer3 = new ArrayList(); - ArrayList
iSecretCards = new ArrayList(); - for (int i = 0; i < cards.size(); i++) {
- if (i >= 51) {
- iSecretCards.add(cards.get(i));
- } else {
- if (i % 3 == 0) {
- iPlayer.add(cards.get(i));
- } else if (i % 3 == 1) {
- iPlayer2.add(cards.get(i));
- } else {
- iPlayer3.add(cards.get(i));
- }
- }
- }
- Collections.sort(iPlayer);
- Collections.sort(iPlayer2);
- Collections.sort(iPlayer3);
- ArrayList
sPlayer = new ArrayList(); - ArrayList
sPlayer2 = new ArrayList(); - ArrayList
sPlayer3 = new ArrayList(); - ArrayList
sSecretCards = new ArrayList(); - for (Integer key : iPlayer) {
- sPlayer.add(map.get(key));
- }
- for (Integer key : iPlayer2) {
- sPlayer2.add(map.get(key));
- }
- for (Integer key : iPlayer3) {
- sPlayer3.add(map.get(key));
- }
- for (Integer key : iSecretCards) {
- sSecretCards.add(map.get(key));
- }
- // 显示
- System.out.println("玩家1:" + sPlayer);
- System.out.println("玩家2:" + sPlayer2);
- System.out.println("玩家3:" + sPlayer3);
- System.out.println("底牌:" + sSecretCards);
- }
- }