InputData.java
1 package vote; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 7 public class InputData { 8 9 private BufferedReader buf ; 10 11 public InputData() 12 { 13 this.buf = new BufferedReader(new InputStreamReader(System.in)); 14 } 15 16 public String getString(String info) 17 { 18 String str = null; 19 System.out.println(info); 20 try { 21 str = this.buf.readLine(); 22 } catch (IOException e) { 23 // TODO 自动生成的 catch 块 24 e.printStackTrace(); 25 } 26 return str; 27 28 } 29 30 public int getInt(String info,String error) 31 { 32 int temp = 0; 33 String str = null; 34 str = this.getString(info); 35 if(str.matches("\\d+")){ 36 temp = Integer.parseInt(str); 37 }else{ 38 System.out.println(error); 39 } 40 41 return temp; 42 } 43 44 45 }
Person.java
1 package vote; 2 3 public class Person implements Comparable<Person>{ 4 private String name; 5 private int stu_Id; 6 7 public Person(String name, int stu_Id, int voteNum) { 8 super(); 9 this.name = name; 10 this.stu_Id = stu_Id; 11 this.voteNum = voteNum; 12 } 13 14 public String getName() { 15 return name; 16 } 17 public void setName(String name) { 18 this.name = name; 19 } 20 public int getStu_Id() { 21 return stu_Id; 22 } 23 public void setStu_Id(int stu_Id) { 24 this.stu_Id = stu_Id; 25 } 26 public int getVoteNum() { 27 return voteNum; 28 } 29 public void setVoteNum(int voteNum) { 30 this.voteNum = voteNum; 31 } 32 private int voteNum; 33 34 @Override 35 public int compareTo(Person o) { 36 // TODO 自动生成的方法存根 37 if(this.getVoteNum() > o.getVoteNum()) 38 { 39 return -1; 40 }else if(this.getVoteNum() < o.getVoteNum()) 41 { 42 return 1; 43 }else 44 { 45 return 0; 46 } 47 } 48 }
VoteOperate.java
1 package vote; 2 3 import java.io.File; 4 import java.util.Arrays; 5 6 public class VoteOperate { 7 private Person[] per = {new Person("小米",001,0),new Person("大米",002,0), new Person("玉米",003,0), 8 new Person("稀饭",004,0),new Person("刘能",005,0),new Person("逼",006,0)}; 9 10 private InputData input ; 11 private boolean flag = true; 12 private int sum; 13 private int errorVote; 14 15 public VoteOperate() 16 { 17 this.input = new InputData(); 18 while(flag) 19 { 20 ++sum; 21 this.voteForCandidate(); 22 } 23 this.printInfo(); 24 this.getResulet(); 25 } 26 27 public void voteForCandidate() 28 { 29 int temp = 0; 30 temp = input.getInt("请输入候选人编号(0表示投票结束)", "投票只能是数字,范围0-" + per.length); 31 switch (temp) { 32 case 1: 33 { 34 this.per[0].setVoteNum(this.per[0].getVoteNum() + 1); 35 break; 36 } 37 38 case 2: 39 { 40 this.per[1].setVoteNum(this.per[1].getVoteNum() + 1); 41 break; 42 } 43 44 case 3: 45 { 46 this.per[2].setVoteNum(this.per[2].getVoteNum() + 1); 47 break; 48 } 49 50 case 4: 51 { 52 this.per[3].setVoteNum(this.per[3].getVoteNum() + 1); 53 break; 54 } 55 56 case 5: 57 { 58 this.per[4].setVoteNum(this.per[4].getVoteNum() + 1); 59 break; 60 } 61 case 6: 62 { 63 this.per[5].setVoteNum(this.per[5].getVoteNum() + 1); 64 break; 65 } 66 case 0: 67 { 68 System.out.println("退出系统"); 69 this.flag = false; 70 break; 71 } 72 default:{ 73 System.out.println("请重新输入一个数字:"); 74 this.errorVote++; 75 } 76 } 77 } 78 79 //打印候选人信息: 80 public void printInfo(){ 81 for(int i=0;i<per.length;i++){ 82 System.out.println(this.per[i].getStu_Id() + ":" 83 + this.per[i].getName() + "【"+this.per[i].getVoteNum()+"】") ; 84 } 85 } 86 87 //获取结果 88 public void getResulet(){ 89 Arrays.sort(per); 90 if ((sum - 1 )==0) 91 { 92 System.out.println("投票故障..."); 93 } 94 else 95 { 96 System.out.println("投票最终结果:" + "\n共投出:" + ( this.sum - 1 )+ "票,其中,错误投票 : " + this.errorVote + "票,有效票" + ( this.sum - 1 - this.errorVote) + "\n" 97 + this.per[0].getName()+"同学,最后以"+this.per[0].getVoteNum()+"票当选班长!") ; 98 } 99 }; 100 101 }
Test.java
1 package vote; 2 3 public class Test { 4 public static void main(String[] args) { 5 new VoteOperate(); 6 } 7 }
效果截图
附:
原文:https://www.cnblogs.com/ftl1012/p/9326283.html