编辑代码

import java.util.Scanner;
class Main {
	public static void main(String[] args) {
        //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。
		System.out.println("欢迎你进入班级投票系统");
        boolean flg = true;//可以继续投票,如果想人为中断投票,将其设置为false即可
        Scanner scan = new Scanner(System.in);// 从键盘接收数据
        while(flg){
            System.out.println("1.继续投票 2.投票中断");
            int choice = scan.nextInt();//接受数字,赋值给choice
            if(choice==1){
                //继续投票
                System.out.println("1.创建学生 2.投票");
                int ncho = scan.nextInt();
                if(ncho==1){
                    if(Bj.bjxsxe<Bj.bjxsde){
                        System.out.println("请输入学生学号:");//如果学生信息重复怎么办?
                        Scanner scan1 = new Scanner(System.in);// 从键盘接收数据
                        String xh = scan1.nextLine();
                        scan1.close();
                        System.out.println("请输入学生姓名:");
                        Scanner scan2 = new Scanner(System.in);// 从键盘接收数据
                        String xm = scan2.nextLine();
                        scan2.close();
                        Student student = new Student(xh,xm,"2班");
                        Bj.student[Bj.bjxsxe] = student;//把学生加入班级
                        Bj.bjxsxe = Bj.bjxsxe + 1;
                    }
                    else {
                        System.out.println("学生班级已经满员,不能创建新的学生。");
                    }
                }
                else if(ncho==2){
                    System.out.println("请输入投票学生的学号:");
                    String xh = scan.nextLine();
                    for(int i=0;i<Bj.bjxsxe;i++){
                        if(Bj.student[i].getXH()==xh){
                            Bj.student[i].touPiao();
                        }
                    }
                }
            }
            else if(choice==2){//人为中断
                flg = false;//设置投票中断
            }
            else {
                System.out.println("你的选择有误,请重新选择");
            }
            
        }
        scan.close();
	}
}
class Student{
    //1.学号 2.姓名 3.班级 4.是否投票 5.学生得票数 
    private String xh;
    private String xm;
    private String bj;
    private boolean istp;
    private int dp;

    public String getXH(){
        return this.xh;
    }

    public void setDP(){
        this.dp = this.dp + 1;
    }

    public int getDP(){
        return this.dp;
    }

    public Student(){

    }
    public Student(String xh,String xm,String bj){
        this.xh = xh;
        this.xm = xm;
        this.bj = bj;
    }

    //投票方法
    public void touPiao(){
        if(this.istp){
            System.out.println("请勿重复投票!");
            return;
        }
        //如何保存该投票,把票投给谁?
        System.out.println("请输入投票给哪位同学,输入学号即投票:");
        Scanner scan = new Scanner(System.in);// 从键盘接收数据
        String xh = scan.nextLine();
        scan.close();
        for(int i=0;i<Bj.bjxsxe;i++){
            if(Bj.student[i].getXH()==xh){
                Bj.student[i].setDP();
            }
        }

        this.istp = true;
        System.out.println("投票成功!");
    }
}
class Bj {
    public static String bjmc="2班";
    public static int bjxsde = 3;
    public static int bjxsxe = 0;
    public static Student[] student = new Student[10];//保存新建的学生
}