static变量的应用:模拟选民投票一人一票,100截止

//Voter类

package cn.com.ooa3.staticdemo;

//模拟选民投票,每人一票,100截止
public class Voter {
    //目前投票数,被所有选民实例所共享
    static int count;
    //投票总数最大值,到100时,就停止投票
    static final int MAX_COUNT=100;
    //选民名字
    private String name;


    public Voter(){}
    public Voter(String name){
        this.name=name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //选民投票
    public void vote(){
        if(count==100){
            System.out.println("投票数已达到100,投票已停止!");
            return;
        }else{
            count++;
            System.out.println(this.name+"选民投票成功!当前投票总数为:"+count);

        }

    }
}

//TestVoter类

package cn.com.ooa3.staticdemo;

public class TestVoter {
    public static void main(String[] args) {
        Voter v1=new Voter("张三");
        v1.vote();
        Voter v2=new Voter("李四");
        v2.vote();
        Voter v3=new Voter("王五");
        v3.vote();


        for (int i=0;i<=97;i++){
            Voter v=new Voter("v"+(i+1));
            v.vote();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值