java:投個票程序
阿新 • • 發佈:2017-08-27
pat [] pen err 姓名 自動 throw 數據 this
投票城市用到了:system.in, 正則pattern,matcher,排序接口comparable 復寫compareTo排序方法
一個班級在選班長,按序號進行投票,並將票數最高的放在第一位顯示
設計:
student學生類
投票菜單類
投票操作類
接受輸入數據類
主程序調用
public class Student implements Comparable{ private int id; private String name; private int vote; public Student(int id, String name, int vote) { super(); this.id = id; this.name = name; this.vote = vote; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getVote() { return vote; } public void setVote(int vote) { this.vote = vote; } @Override public String toString() { return "姓名::" + name + ", 票數:" + vote + "]"; } public int compareTo(Student stu) { // TODO 自動生成的方法存根 if(this.vote > stu.vote) { return 1; }else if(this.vote < stu.vote) { return -1; }else{ return 0; } } }
public class Menu { private Student stu[] = {new Student(1,"張三",0), new Student(2,"李四",0), new Student(3,"王五",0), new Student(4,"田七",0)}; public Menu() throws IOException { Operater oper = new Operater(this.stu); //輸出全部需要投票的姓名 oper.list(); //投票 while(oper.vote()) { ; } //投票結束後,列出所有的飄數 oper.list(); } }
ublic class Operater { private Student stu[]=null; private InputData inputData = null; public Operater(Student stu[]) { this.stu = stu; this.inputData = new InputData(); } public void list() { for(int i =0; i< this.stu.length; i++) { System.out.println(this.stu[i].getId()+"號,姓名:"+this.stu[i].getName()+":【"+this.stu[i].getVote()+"】票"); } } public boolean vote() throws IOException { boolean flag = true; int stuno = this.inputData.getInt("請輸入你要選擇的班長候選人ID:", "抱歉您輸入的數據有誤"); switch(stuno) { case 1:{ this.stu[0].setVote( this.stu[0].getVote() + 1); break; } case 2:{ this.stu[1].setVote( this.stu[1].getVote() + 1); break; } case 3:{ this.stu[2].setVote( this.stu[2].getVote() + 1); break; } case 4:{ this.stu[3].setVote( this.stu[3].getVote() + 1); break; } case 0: { flag = false; break; } default: { flag = false; break; } } return flag; } }
public class InputData { /*public static void main(String[] args) throws IOException { InputStream ipt = System.in; System.out.println("請輸入:"); int temp=0; StringBuffer buf = new StringBuffer(); while( (temp = ipt.read()) != -1 ) { char c = (char)temp; if(c == ‘\n‘) { break; } buf.append(c); } String IpuStr = new String(buf); //IpuStr = IpuStr.matches("\\s*|\r|\n|\t"); System.out.println( replace(IpuStr) ); IpuStr = replace(IpuStr); if(IpuStr.matches("^\\d$")) { System.out.println( new Integer(IpuStr) ); }else{ System.out.println( "0" ); } } public static String replace(String str) { if(str != null && !"".equals(str)) { Pattern p = Pattern.compile("\\s*|\r|\n|\t"); Matcher m = p.matcher(str); return m.replaceAll(""); }else{ return str; } } */ public String replace(String str) { if(str != null && !"".equals(str)) { Pattern p = Pattern.compile("\\s*|\r|\n|\t"); Matcher m = p.matcher(str); return m.replaceAll(""); }else{ return str; } } public int getInt(String str, String str2) throws IOException { InputStream ipt = System.in; System.out.println(str); int temp=0; StringBuffer buf = new StringBuffer(); while( (temp = ipt.read()) != -1 ) { char c = (char)temp; if(c == ‘\n‘) { break; } buf.append(c); } String IpuStr = new String(buf); IpuStr = this.replace(IpuStr); if(IpuStr.matches("\\d")) { return new Integer(IpuStr); }else{ return 0; } } }
public class Test { public static void main(String[] args) throws IOException { // TODO 自動生成的方法存根 new Menu(); } }
java:投個票程序