45 lines
1.1 KiB
Java
45 lines
1.1 KiB
Java
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();
|
||
}
|
||
}
|