java:投個票程式

來源:互聯網
上載者:User

標籤:nts   主程式   put   sharp   bsp   private   產生   log   stat   

投票城市用到了: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;}@Overridepublic 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:投個票程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.