static静态变量-投票案例

 1 public class Voter {
 2     String name;  //名字
 3     private static int count;  //投票数
 4     
 5     public Voter() {}
 6     
 7     public Voter(String name) {
 8         this.name=name;
 9     }
10     
11     public void Vote() {
12         if(count==100) {
13             System.out.println("投票活动已经结束!");
14             return;
15         }else {
16             count++;
17             System.out.println(this.name+"欢迎您投票,当前票数是:"+count);
18         }
19     }
20 }
 1 //选民投票,每人只允许投票一次,当票数达到100时停止投票
 2 public class TestVoter {
 3 
 4     public static void main(String[] args) {
 5         for(int i=0;i<105;i++) {
 6             Voter v=new Voter("张三"+(i+1));
 7             v.Vote();
 8         }
 9     }
10 
11 }

猜你喜欢

转载自www.cnblogs.com/baichang/p/10073968.html