diff --git a/src/chapter3/Example14.java b/src/chapter3/Example14.java new file mode 100644 index 0000000..19c4dd5 --- /dev/null +++ b/src/chapter3/Example14.java @@ -0,0 +1,2 @@ +package chapter3;public class Example14 { +} diff --git a/src/chapter3/demo34/Main.java b/src/chapter3/demo34/Main.java new file mode 100644 index 0000000..ff9e408 --- /dev/null +++ b/src/chapter3/demo34/Main.java @@ -0,0 +1,16 @@ +package chapter3.demo34; + +/** + * Main类,程序的入口点 + */ +public class Main { + /** + * 主方法 + * @param args 命令行参数 + */ + public static void main(String[] args) { + // 创建VotingSystem对象并开始投票 + VotingSystem votingSystem = new VotingSystem(); + votingSystem.startVoting(); + } +} diff --git a/src/chapter3/demo34/Voter.java b/src/chapter3/demo34/Voter.java new file mode 100644 index 0000000..34d1cc1 --- /dev/null +++ b/src/chapter3/demo34/Voter.java @@ -0,0 +1,95 @@ +package chapter3.demo34; + +/** + * Voter类代表一个投票者 + */ +public class Voter { + // 最大投票人数 + private static final int MAX_COUNT = 50; + // 当前投票人数 + private static int count = 0; + // 存储所有投票者的数组 + private static Voter[] voters = new Voter[MAX_COUNT]; + + // 投票者姓名 + private String name; + // 投票意见(true表示赞同,false表示反对) + private boolean opinion; + + /** + * 构造方法 + * @param name 投票者姓名 + */ + public Voter(String name) { + this.name = name; + } + + /** + * 投票方法 + * @param opinion 投票意见 + */ + public void vote(boolean opinion) { + // 检查是否达到最大投票人数 + if (count >= MAX_COUNT) { + System.out.println("投票已结束,已达到最大投票数。"); + return; + } + + // 检查是否重复投票 + if (hasVoted(this.name)) { + System.out.println(name + ",请勿重复投票。"); + return; + } + + // 记录投票意见 + this.opinion = opinion; + voters[count] = this; + count++; + System.out.println(name + ",感谢您的投票。"); + } + + /** + * 检查指定姓名的人是否已经投票 + * @param name 要检查的姓名 + * @return 如果已投票返回true,否则返回false + */ + private static boolean hasVoted(String name) { + for (int i = 0; i < count; i++) { + if (voters[i].name.equals(name)) { + return true; + } + } + return false; + } + + /** + * 打印投票结果 + */ + public static void printResults() { + System.out.println("===========投票结果==========="); + System.out.println("总投票数:" + count); + int agreeCount = 0; + for (int i = 0; i < count; i++) { + System.out.println(voters[i].name + " 的意见:" + (voters[i].opinion ? "赞同" : "反对")); + if (voters[i].opinion) { + agreeCount++; + } + } + System.out.println("赞同人数:" + agreeCount); + System.out.println("反对人数:" + (count - agreeCount)); + } + + /** + * 获取投票者姓名 + * @return 投票者姓名 + */ + public String getName() { + return name; + } + + // 静态初始化块,在类加载时执行 + static { + System.out.println("Voter类已加载,准备开始投票。"); + } +} + diff --git a/src/chapter3/demo34/VotingSystem.java b/src/chapter3/demo34/VotingSystem.java new file mode 100644 index 0000000..7c7507e --- /dev/null +++ b/src/chapter3/demo34/VotingSystem.java @@ -0,0 +1,44 @@ +package chapter3.demo34; + +import java.util.Scanner; + +/** + * VotingSystem类管理整个投票过程 + */ +public class VotingSystem { + // 用于接收用户输入 + private Scanner scanner; + + /** + * 构造方法 + */ + public VotingSystem() { + this.scanner = new Scanner(System.in); + } + + /** + * 开始投票过程 + */ + public void startVoting() { + while (true) { + // 输入投票者姓名 + System.out.println("请输入您的姓名:"); + String name = scanner.nextLine(); + Voter voter = new Voter(name); + + // 输入投票意见 + System.out.println("请投票(Y表示赞同,N表示反对):"); + String opinion = scanner.nextLine(); + voter.vote(opinion.equalsIgnoreCase("Y")); + + // 询问是否继续投票 + System.out.println("是否继续投票?(Y/N)"); + if (scanner.nextLine().equalsIgnoreCase("N")) { + break; + } + } + + // 打印投票结果 + Voter.printResults(); + } +}