class
Student{
private
int
id
;
private
String
name
;
private
int
count
;
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
getCount() {
return
count
;
}
public
void
setCount(
int
count) {
this
.
count
= count;
}
public
Student(
int
id, String name,
int
count) {
this
.
id
= id;
this
.
name
= name;
this
.
count
= count;
}
}
public
class
IOtest {
public
static
void
main(String[] args)
throws
Exception{
Scanner scanner=
new
Scanner(System.
in
);
Student student1=
new
Student(1,
"张三"
,0);
Student student2=
new
Student(2,
"李四"
,0);
Student student3=
new
Student(3,
"王五"
,0);
Student student4=
new
Student(4,
"赵六"
,0);
Student students[]={student1,student2,student3,student4};
for
(Student student:students){
System.
out
.println(student.getId()+
": "
+student.getName()+
" 【"
+student.getCount()+
"】"
);
}
int
a=1;
int
num1=0,num2=0,num3=0,num4=0;
while
(a!=0){
String str= scanner.next();
if
(str.matches(
"\\d+"
)){
a=Integer. parseInt(str);
System.
out
.println(
"请输入班长候选人代号(数字0结束):"
+a);
if
(a==0){
Map map=
new
HashMap();
for
(Student student:students){
System.
out
.println(student.getId()+
": "
+student.getName()+
" 【"
+student.getCount()+
"】"
);
map.put(student.getName(), student.getCount());
}
List>ma=
new
ArrayList>(map.entrySet());
Collections. sort(ma,
new
Comparator>() {
@Override
public
int
compare(Entry o1,
Entry o2) {
if
(o1.getValue()!=
null
&&o2.getValue()!=
null
&&o1.getValue()>=o2.getValue()){
return
1;
}
else
{
return
-1;
}
}
});
System.
out
.println(ma.get(3).getKey()+
"同学,最后以"
+ma.get(3).getValue()+
"当选班长"
);
return
;
}
if
(a==1){
num1++;
students[a-1].setCount(num1);
}
else
if
(a==2){
num2++;
students[a-1].setCount(num2);
}
else
if
(a==3){
num3++;
students[a-1].setCount(num3);
}
else
if
(a==4){
num4++;
students[a-1].setCount(num4);
}
else
{
System.
out
.println(
"此次选票无效,请输入正确的候选人代号"
);
}
}
else
{
System.
out
.println(
"此次选票无效,请输入正确的候选人代号"
);
System.
out
.println(
"请输入班长候选人代号(数字0结束):"
+str);
}
}
}
}