package com.itheima;
import java.util.HashSet;
import java.util.Set;
class Voter{
private static int MAX_VOTERS = 100;
private static int count;
private static Set<Voter> voterSet = new HashSet<Voter>();
private String name;
private String advice;
public Voter(String name){
this.name = name;
}
public void vote(String advice){
if(count == MAX_VOTERS){
System.out.println("投票结束。");
return;
}
if(!voterSet.contains(this)){
this.advice = advice;
count++;
voterSet.add(this);
System.out.println(name + "已完成投票");
}
else
{
System.out.println(name + ", 一人仅能投票一次哦~");
}
}
public static void show(){
System.out.println("当前投票数: "+ count);
System.out.println("本次投票结果:");
for(Voter voter : voterSet)
{
System.out.println(voter.name + ", 意见:"+ voter.advice);
}
}
}
public class Main{
public static void main(String[] args){
Voter james = new Voter("james");
Voter durant = new Voter("Durant");
Voter curry = new Voter("Curry");
james.vote("是");
durant.vote("否");
durant.vote("否");
curry.vote("是");
Voter.show();
}
}
java基础案例3-4学生投票系统
于 2022-02-09 01:03:07 首次发布