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 }

 

转载于:https://www.cnblogs.com/baichang/p/10073968.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值