public class Voter1 { //定义属性 private String name; static int count; static int MAX_COUNT = 100; //定义方法 //方法1判断是否超过100 public Voter1(){ } public Voter1(String name){ this.name = name; } public void vote(){ if(this.count == MAX_COUNT){ System.out.println("投票活动已结束"); }else{ count++; System.out.println(this.name+":感谢您投票!"); } } public static void print(){ System.out.println("选民投票总数是"+count); } }
public class TestVoter1 { public static void main(String[] args){ for(int i=1;i<=100;i++){ Voter1 voter1 = new Voter1("v"+i); voter1.vote(); } Voter1.print(); } }