日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

投票选举

發布時間:2024/1/18 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 投票选举 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

有一個班采用民主投票方法推選班長,班長候選人共4位,每個人姓名及代號分別為“張三? 1;李四? 2;王五? 3;趙六? 4”。程序操作員將每張選票上所填的代號(1、2、3或4)循環輸入電腦,輸入數字0結束輸入,然后將所有候選人的得票情況顯示出來,并顯示最終當選者的信息,具體要求如下:

A、要求用面向對象方法,編寫學生類Student,將候選人姓名、代號和票數保存到類Student中,并實現相應的getXXX 和 setXXX方法。

B、輸入數據前,顯示出各位候選人的代號及姓名(提示,建立一個候選人類型數組)。

C、循環執行接收鍵盤輸入的班長候選人代號,直到輸入的數字為0,結束選票的輸入工作。

D、在接收每次輸入的選票后要求驗證該選票是否有效,即如果輸入的數不是0、1、2、3、4這5個數字之一,或者輸入的是一串字母,應顯示出錯誤提示信息“此選票無效,請輸入正確的候選人代號!”,并繼續等待輸入。

E、輸入結束后顯示所有候選人的得票情況,如參考樣例所示。

F、輸出最終當選者的相關信息,如參考樣例所示。

1:張三【0票】 2:李四【0票】 3:王五【0票】 4:趙六【0票】 請輸入班長候選人代號(數字0結束):1 請輸入班長候選人代號(數字0結束):1 請輸入班長候選人代號(數字0結束):1 請輸入班長候選人代號(數字0結束):2 請輸入班長候選人代號(數字0結束):3 請輸入班長候選人代號(數字0結束):4 請輸入班長候選人代號(數字0結束):5 此選票無效,請輸入正確的候選人代號! 請輸入班長候選人代號(數字0結束):hello 此選票無效,請輸入正確的候選人代號! 請輸入班長候選人代號(數字0結束):01:張三【4票】2:李四【1票】3:王五【1票】4:趙六【1票】

投票最終結果:張三同學,最后以4票當選班長!

?

?

1、 建立學生類,這個類里面需要保存有編號、姓名、票數。

1 package com.baidu.demo.vote; 2 3 public class Student implements Comparable<Student>{ 4 private long sid; 5 private String name; 6 private int vote; 7 public Student(long sid,String name,int vote) { 8 this.sid = sid; 9 this.name = name; 10 this.vote = vote; 11 } 12 @Override 13 public String toString() { 14 return "【" + this.sid + "】" + this.name + "、票數" + this.vote; 15 } 16 @Override 17 public int compareTo(Student student) { 18 return student.vote - this.vote; 19 } 20 public long getSid() { 21 return this.sid; 22 } 23 public void setSid(long sid) { 24 this.sid = sid; 25 } 26 public String getName() { 27 return this.name; 28 } 29 public void setName(String name) { 30 this.name = name; 31 } 32 public int getVote() { 33 return this.vote; 34 } 35 public void setVote(int vote) { 36 this.vote = vote; 37 } 38 }

?

2、 定義投票處理的業務接口

1 package com.baidu.demo.service; 2 3 import com.baidu.demo.vote.Student; 4 5 public interface IVoteService { 6 public boolean increase(long sid); 7 public Student[] result(); 8 public Student[] getData(); 9 }

?

3、 定義VoteServiceImpl子類。

1 package com.baidu.demo.service; 2 3 import java.util.Arrays; 4 import com.baidu.demo.vote.Student; 5 6 public class VoteServiceImpl implements IVoteService { 7 private Student[] students = new Student[] { 8 new Student(1, "張三", 0), 9 new Student(2, "李四", 0), 10 new Student(3, "王五", 0), 11 new Student(4, "趙六", 0) 12 }; 13 14 @Override 15 public boolean increase(long sid) { 16 for(int x=0;x<students.length;x++) { 17 if(this.students[x].getSid() == sid) { 18 this.students[x].setVote(this.students[x].getVote() + 1); 19 return true; 20 } 21 } 22 return false; 23 } 24 25 @Override 26 public Student[] result() { 27 Arrays.sort(this.students); 28 return this.students; 29 } 30 31 @Override 32 public Student[] getData() { 33 return this.students; 34 } 35 }

?

4、 定義工廠類

1 package com.baidu.demo.factory; 2 3 import com.baidu.demo.service.IVoteService; 4 import com.baidu.demo.service.VoteServiceImpl; 5 6 public class Factory { 7 private Factory() {} 8 public static IVoteService getInstance() { 9 return new VoteServiceImpl(); 10 } 11 }

?

5、 定義一個工具類

1 package com.baidu.demo.util; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 7 public class InputUtil { 8 private static final BufferedReader INPUT = new BufferedReader(new InputStreamReader(System.in)); 9 private InputUtil() {} 10 public static String getString(String prompt) { 11 String str = null; 12 boolean flag = true; 13 while(flag) { 14 System.out.println(prompt); 15 try { 16 str = INPUT.readLine().trim(); 17 if(!"".equals(str)) { 18 flag = false; 19 }else { 20 System.out.println("輸入的內容不能為空!"); 21 } 22 } catch (Exception e) { 23 System.out.println("輸入的內容不能為空!"); 24 } 25 } 26 return str; 27 } 28 29 public static int getInt(String prompt) { 30 BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)) ; 31 int num = 0 ; 32 boolean flag = true ; 33 while (flag) { 34 System.out.print(prompt); // 打印提示信息 35 String str = null ; 36 try { 37 str = buf.readLine() ; 38 if (str.matches("\\d+")) { 39 num = Integer.parseInt(str) ; 40 flag = false ; 41 } else { 42 System.out.println("輸入的內容不是數字!"); 43 } 44 } catch (IOException e) { 45 System.out.println("輸入的內容不是數字!"); 46 } 47 } 48 return num ; 49 } 50 }

?

6、 定義一個菜單的信息顯示類

1 package com.baidu.demo.menu; 2 3 import com.baidu.demo.factory.Factory; 4 import com.baidu.demo.service.IVoteService; 5 import com.baidu.demo.vote.Student; 6 import com.baidu.demo.util.InputUtil; 7 8 public class Menu { 9 private IVoteService voteService; 10 public Menu() { 11 this.voteService = Factory.getInstance(); 12 this.vote(); 13 } 14 public void vote() { 15 Student student[] = this.voteService.getData(); 16 for(Student temp: student) { 17 System.out.println(temp.getSid()+":"+temp.getName()+"【"+temp.getVote()+"】"); 18 } 19 int num = 10; 20 while (num != 0) { 21 num = InputUtil.getInt("請輸入班長候選人帶好(數字0結束):"); 22 if(num != 0) { 23 if(!this.voteService.increase(num)) { 24 System.out.println("此選票無效,請輸入正確的候選代號!"); 25 } 26 } 27 } 28 System.out.println("投票最終結果:"); 29 student = this.voteService.result(); 30 System.out.println(student[0].getName()+"同學,以"+student[0].getVote()+"票當選班長。"); 31 } 32 }

?

7、 定義測試類

1 package com.baidu.demo; 2 3 import com.baidu.demo.menu.Menu; 4 5 public class IOCaseDemo { 6 public static void main(String[] args) { 7 new Menu(); 8 } 9 }

?

轉載于:https://www.cnblogs.com/sunzhongyu008/p/11214785.html

總結

以上是生活随笔為你收集整理的投票选举的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。