Java2024/src/chapter3/demo34/VotingSystem.java
2024-10-13 20:28:47 +08:00

45 lines
1.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
}
}