zoukankan      html  css  js  c++  java
  • 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
    马健
  • 相关阅读:
    CodeForces 733B Parade
    LeetCode 150 Evaluate Reverse Polish Notation
    LeetCode 148 Sort List
    数据库的迁移
    LeetCode 147. Insertion Sort List
    构建一个可以统计 qps 的nginx服务的Dockerfile
    各城市区号
    tkinter中menu菜单控件(十二)
    tkinter中scale拖拉改变值控件(十一)
    tkinter中spinbox递增和递减控件(十)
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5697572.html
Copyright © 2011-2022 走看看