static关键字 综合小应用

package unit4;

import com.sun.java_cup.internal.version;

public class Voter {
    private static int MAX_COUNT =100;
    private static int count;
    private static Voter[] voters= new Voter[MAX_COUNT];
    private String name;
    public Voter(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void voteFor() {
        if(count==MAX_COUNT){
            System.out.println("投票活动已经结束");
            return ;
        }
        if(isExisted(this)){
            System.out.println("不允许重复投票!");
        }else{
            votersadd(this);
            System.out.println("感谢你投票");
        }
    }
    public static boolean  isExisted(Voter obj) {
        for(int i=0;i<count;i++){
            if(voters[i].getName()==obj.getName()){
                return true;
            }
        }
        return false;
        
    }
    public static void votersadd(Voter obj) {
        voters[count++]=obj;
    }
    public static void  printVoteResult() {
        System.out.println("当前的投票数为:"+count);
        System.out.println("参与投票的选民如下:");
        for(int i=0;i<count;i++){
            System.out.println(voters[i].getName());
        }
    }
    public static  void main(String[] args) {
        Voter majun=new Voter("马军");
        Voter fly = new Voter("飞翔");
        Voter mike = new Voter("mike");
        Voter majian = new Voter("马健");
        majun.voteFor();
        fly.voteFor();
        mike.voteFor();
        mike.voteFor();
        majian.voteFor();
        Voter.printVoteResult();
    }
    
}

输出结果:

感谢你投票
感谢你投票
感谢你投票
不允许重复投票!
感谢你投票
当前的投票数为:4
参与投票的选民如下:
马军
飞翔
mike
马健
原文地址:https://www.cnblogs.com/superxuezhazha/p/5697572.html