当前位置: 移动技术网 > IT编程>开发语言>Java > java实现投票选举统计票数功能

java实现投票选举统计票数功能

2019年07月19日  | 移动技术网IT编程  | 我要评论

有一个班采用民主投票方法推选班长,班长候选人共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结束):0
    1:张三【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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
  2020 -06-14 18:23:15
大佬!
支持: 0 反对: 0 回复
移动技术网