关闭

java投票系统

1103人阅读 评论(0) 收藏 举报
//投票方法
import java.util.Arrays;
import java.util.Scanner;

/**
 * @author 清澈见底的疯子
 *
 */
public class SingVote{  
	private Singer sin[] = {new Singer("张学友",0,0),new Singer("刘德华",1,0),  
			new Singer("小刚",2,0),new Singer("孙燕姿",3,0),new Singer("梁静茹",4,0),
			new Singer("王杰",5,0),new Singer("刘若英",6,0)};
    private boolean flag = true; 
    static class Singer implements Comparable<Singer> {
    	private String Name;
    	private int Num;
    	private int Vote;
    	public Singer(String name, int num, int vote) {
    		super();
    		Name = name;
    		Num = num;
    		Vote = vote;
    	}
    	public String getName() {
    		return Name;
    	}
    	public void setName(String name) {
    		Name = name;
    	}
    	public int getNum() {
    		return Num;
    	}
    	public void setNum(int num) {
    		Num = num;
    	}
    	public int getVote() {
    		return Vote;
    	}
    	public void setVote(int vote) {
    		Vote = vote;
    	}
    	@Override
    	public int compareTo(Singer s) {
    		return s.Vote-this.Vote;
    	}
    }   
    public void ff(){
    	while(true){
    	System.out.println("投票开始:");
    	System.out.println("输入方框数字编号投票给歌手,输入.结束");
    	int count = 0;
        while(flag){
        	System.out.print("张学友[0],刘德华[1],小刚[2],孙燕姿[3],梁静茹[4],王杰[5],刘若英[6])");
            this.vote(); 
            count++;
        }  
        this.printInfo();  
        this.getResult();
        int sum = 0;
        sum = this.sin[0].getVote()+this.sin[1].getVote()+this.sin[2].getVote()+this.sin[3].getVote()+
        	  this.sin[4].getVote()+this.sin[5].getVote()+this.sin[6].getVote();
        System.out.println();
        System.out.println("弃权票数:"+(count-sum-1));
        System.out.println("总票数:"+sum);
        System.out.println("继续第二轮投票?(y/n)");
        @SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
        String y = scan.nextLine();
        if(y.equals("n")){
        	break;
        }
        flag = true;
    	}
    }
    private void getResult(){  
    	System.out.print("\n");
        Arrays.sort(sin);
        System.out.println("歌手票数排行榜:");
        System.out.println("==========");
        for(int i=0;i<sin.length;i++){
        System.out.println(this.sin[i].getName()+"\t"+this.sin[i].getVote());
        }
        System.out.println("投票最终结果:" + this.sin[0].getName()+",最后以"+this.sin[0].getVote()+"票获奖!");
    }  
    public void vote(){ 
        InputData input = new InputData();
        String num = input.getString("?");
        switch(num){  
            case ".":{  
                this.flag = false;  
                System.out.println("投票结束,结果是:"+"\n");
                System.out.println("歌手"+"\t"+"得票数");
                System.out.println("==="+"\t"+"====");
                break ;  
            }  
            case "0":{  
                this.sin[0].setVote(this.sin[0].getVote() + 1);  
                break ;  
            }  
            case "1":{  
                this.sin[1].setVote(this.sin[1].getVote() + 1);  
                break ;  
            }  
            case "2":{  
                this.sin[2].setVote(this.sin[2].getVote() + 1);  
                break ;  
            }  
            case "3":{  
                this.sin[3].setVote(this.sin[3].getVote() + 1);  
                break ;  
            } 
            case "4":{  
                this.sin[4].setVote(this.sin[4].getVote() + 1);  
                break ;  
            }  
            case "5":{  
                this.sin[5].setVote(this.sin[5].getVote() + 1);  
                break ;  
            }  
            case "6":{  
                this.sin[6].setVote(this.sin[6].getVote() + 1);  
                break ;  
            }  
        } 
    }  
    public void printInfo(){      
        for(int i=0;i<sin.length;i++){  
            System.out.println(this.sin[i].getName()+"\t"+this.sin[i].getVote()); 
        }  
    }
} 





//定义输入类
/**
 * @author 清澈见底的疯子
 *
 */
import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import java.io.IOException;  
public class InputData{
	private BufferedReader buf = null;
	public InputData(){
		this.buf = new BufferedReader(new InputStreamReader(System.in));
	}
	public String getString(String info){
		String t = null;
		System.out.print(info);
		try{
			t = this.buf.readLine();
		}catch(IOException e){
			e.printStackTrace();
		}
		return t;
	}
}




//main方法
/**
 * @author 清澈见底的疯子
 *
 */
public class VoteStart{  
    public static void main(String args[]){  
        SingVote yy = new SingVote() ; 
        yy.ff();
    }  
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:18040次
    • 积分:478
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:31篇
    • 译文:0篇
    • 评论:0条