您的位置:首页 > 编程语言 > Java开发

java投票系统

2015-11-20 19:37 471 查看
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175


//投票方法
import java.util.Arrays;
import java.util.Scanner;

/**
* @author 清澈见底的疯子
*
*/
public class SingVote{
private Singer sin[] = {new Singer("张学友",0,0),new Singer("刘德华",1,0),
new Singer("小刚",2,0),new Singer("孙燕姿",3,0),new Singer("梁静茹",4,0),
new Singer("王杰",5,0),new Singer("刘若英",6,0)};
private boolean flag = true;
static class Singer implements Comparable<Singer> {
private String Name;
private int Num;
private int Vote;
public Singer(String name, int num, int vote) {
super();
Name = name;
Num = num;
Vote = vote;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getNum() {
return Num;
}
public void setNum(int num) {
Num = num;
}
public int getVote() {
return Vote;
}
public void setVote(int vote) {
Vote = vote;
}
@Override
public int compareTo(Singer s) {
return s.Vote-this.Vote;
}
}
public void ff(){
while(true){
System.out.println("投票开始:");
System.out.println("输入方框数字编号投票给歌手,输入.结束");
int count = 0;
while(flag){
System.out.print("张学友[0],刘德华[1],小刚[2],孙燕姿[3],梁静茹[4],王杰[5],刘若英[6])");
this.vote();
count++;
}
this.printInfo();
this.getResult();
int sum = 0;
sum = this.sin[0].getVote()+this.sin[1].getVote()+this.sin[2].getVote()+this.sin[3].getVote()+
this.sin[4].getVote()+this.sin[5].getVote()+this.sin[6].getVote();
System.out.println();
System.out.println("弃权票数:"+(count-sum-1));
System.out.println("总票数:"+sum);
System.out.println("继续第二轮投票?(y/n)");
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
String y = scan.nextLine();
if(y.equals("n")){
break;
}
flag = true;
}
}
private void getResult(){
System.out.print("\n");
Arrays.sort(sin);
System.out.println("歌手票数排行榜:");
System.out.println("==========");
for(int i=0;i<sin.length;i++){
System.out.println(this.sin[i].getName()+"\t"+this.sin[i].getVote());
}
System.out.println("投票最终结果:" + this.sin[0].getName()+",最后以"+this.sin[0].getVote()+"票获奖!");
}
public void vote(){
InputData input = new InputData();
String num = input.getString("?");
switch(num){
case ".":{
this.flag = false;
System.out.println("投票结束,结果是:"+"\n");
System.out.println("歌手"+"\t"+"得票数");
System.out.println("==="+"\t"+"====");
break ;
}
case "0":{
this.sin[0].setVote(this.sin[0].getVote() + 1);
break ;
}
case "1":{
this.sin[1].setVote(this.sin[1].getVote() + 1);
break ;
}
case "2":{
this.sin[2].setVote(this.sin[2].getVote() + 1);
break ;
}
case "3":{
this.sin[3].setVote(this.sin[3].getVote() + 1);
break ;
}
case "4":{
this.sin[4].setVote(this.sin[4].getVote() + 1);
break ;
}
case "5":{
this.sin[5].setVote(this.sin[5].getVote() + 1);
break ;
}
case "6":{
this.sin[6].setVote(this.sin[6].getVote() + 1);
break ;
}
}
}
public void printInfo(){
for(int i=0;i<sin.length;i++){
System.out.println(this.sin[i].getName()+"\t"+this.sin[i].getVote());
}
}
}

//定义输入类
/**
* @author 清澈见底的疯子
*
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class InputData{
private BufferedReader buf = null;
public InputData(){
this.buf = new BufferedReader(new InputStreamReader(System.in));
}
public String getString(String info){
String t = null;
System.out.print(info);
try{
t = this.buf.readLine();
}catch(IOException e){
e.printStackTrace();
}
return t;
}
}

//main方法
/**
* @author 清澈见底的疯子
*
*/
public class VoteStart{
public static void main(String args[]){
SingVote yy = new SingVote() ;
yy.ff();
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: