2023.8.15 周二:java
/*假设你有一个管理系统,其中有两个选项:添加新人员和显示人员列表。
你可以创建两个不同的类来处理这两个选项的功能。
首先,创建一个名为 Person
的类,类似于前面提到的类,用于表示人员信息。
然后,创建两个额外的类:AddPersonOption
和 DisplayListOption
,
每个类分别负责处理添加新人员和显示人员列表的功能。*/
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 }
标签:
java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?