当前位置 : 主页 > 手机开发 > 其它 >

简单投票

来源:互联网 收集:自由互联 发布时间:2021-06-19
问题: 1、用面向对象方法 2、输入数据前就显示候选人信息 3、循环接受 直到为0时结束 4、错误提示 5、比较器来实现比较 Student package lynu.edu.cn; public class Student implements ComparableStuden

问题:

  1、用面向对象方法

  2、输入数据前就显示候选人信息

  3、循环接受 直到为0时结束

  4、错误提示

  5、比较器来实现比较

 

Student

package lynu.edu.cn;

public class Student implements Comparable<Student> {
    private int id;
    private String name;
    private int count;
    public Student(int id,String name,int count){
        this.setId(id);
        this.setCount(count);
        this.setName(name);
    }
    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 String toString(){
        return this.name+this.count;
    }
    @Override
    public int compareTo(Student student) {
        if (this.count>student.count) {
            return -1;
        }else if (this.count<student.count) {
            return 1;
        }
        return 0;
    }

}

Test

import java.util.Scanner;

public class Test01 
{
    public static void main(String[] args) 
    {    
        Scanner scanner = new Scanner(System.in);
        Student student1 =new Student(01, "张三", 0);
        Student student2 =new Student(02, "李四", 0);
        Student student3 =new Student(03, "王五", 0);
        Student student4 =new Student(04, "赵六", 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;
        int num2 = 0;
        int num3 = 0;
        int num4 = 0;
        while (a!=0) {
            String str = scanner.next();
            if (str.matches("\\d+")) {
                a = Integer.parseInt(str);
                System. out .println("请输入班长候选人代号(数字0结束):" +a);
                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);
                }if (a>4) {
                    System.out.println("对不起 你输入的不是有效编号 请重新输入!");
                }if (a==0) {
                    for (Student student : students) {
                        System.out.println(student.getId()+":"+student.getName()+""
                                +student.getCount()+"票】");
                    }
                    System.out.println();
                    java.util.Arrays.sort(students);
                    System.out.println("投票最终结果:"+students[0].getName()+"同学以"+students[0].getCount()+"票最终当选");
                    
                    return;
                }
                
            }else {
                System.out.println("对不起 你输入的不是数字 请重新输入!");
                System. out .println("请输入班长候选人代号(数字0结束):" +str);
            }
        }
    }
}
网友评论