菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

2023.8.15 周二:java

复制代码
/*假设你有一个管理系统,其中有两个选项:添加新人员和显示人员列表。

你可以创建两个不同的类来处理这两个选项的功能。

首先,创建一个名为 Person 的类,类似于前面提到的类,用于表示人员信息。

然后,创建两个额外的类:AddPersonOptionDisplayListOption

每个类分别负责处理添加新人员和显示人员列表的功能。*/

 1 public class Person {
 2     private String name;
 3     private int age;
 4 
 5     public Person(String name, int age) {
 6         this.name = name;
 7         this.age = age;
 8     }
 9 
10     // Getter and setter methods for name and age (not shown here)
11 }
12 
13 public class AddPersonOption {
14     public static void addPerson(Person[] people, String name, int age) {
15         // Logic to add a new person to the array
16         // For example: people[numberOfPeople] = new Person(name, age);
17     }
18 }
19 
20 public class DisplayListOption {
21     public static void displayList(Person[] people) {
22         for (Person person : people) {
23             if (person != null) {
24                 System.out.println("Name: " + person.getName() + ", Age: " + person.getAge());
25             }
26         }
27     }
28 }
29 
30 public class Main {
31     public static void main(String[] args) {
32         Person[] people = new Person[5];
33 
34         // Menu loop
35         while (true) {
36             System.out.println("1. Add Person");
37             System.out.println("2. Display List");
38             System.out.println("3. Exit");
39             System.out.print("Choose an option: ");
40 
41             Scanner scanner = new Scanner(System.in);
42             int choice = scanner.nextInt();
43             scanner.nextLine();  // Consume the newline character
44 
45             switch (choice) {
46                 case 1:
47                     System.out.print("Enter name: ");
48                     String name = scanner.nextLine();
49                     System.out.print("Enter age: ");
50                     int age = scanner.nextInt();
51                     AddPersonOption.addPerson(people, name, age);
52                     break;
53                 case 2:
54                     DisplayListOption.displayList(people);
55                     break;
56                 case 3:
57                     System.out.println("Exiting the program. Goodbye!");
58                     return;
59                 default:
60                     System.out.println("Invalid option. Please choose a valid option.");
61                     break;
62             }
63         }
64     }
65 }
复制代码

 

posted on   hhmzd233  阅读(15)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示