〖JAVA IO〗_執行個體操作:投票程式筆記

來源:互聯網
上載者:User

〖JAVA IO〗_執行個體操作:投票程式筆記

 public class Student implements Comparable<Student>{    private int stuNo ;    // 學生編號    private String name ;    // 學生姓名    private int vote ;    // 學生票數    public Student(int stuNo,String name,int vote){        this.setStuNo(stuNo) ;        this.setName(name) ;        this.setVote(vote) ;    }    public int compareTo(Student o){        if(this.vote<o.vote){            return 1 ;        }else if(this.vote>o.vote){            return -1 ;        }else{            return 0 ;        }    }    public void setStuNo(int stuNo){        this.stuNo = stuNo ;    }    public void setName(String name){        this.name = name ;    }    public void setVote(int vote){        this.vote = vote ;    }    public int getStuNo(){        return this.stuNo ;    }    public String getName(){        return this.name ;    }    public int getVote(){        return this.vote ;    } };
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 temp =  null ;    // 接收輸入內容        System.out.print(info) ;        try{            temp = this.buf.readLine() ;    // 接收資料        }catch(IOException e){            e.printStackTrace() ;        }        return temp ;    }    public int getInt(String info,String err){    // 得到整型資料        int temp = 0 ;        String str = null ;        boolean flag = true ;    // 定義一個迴圈標記        while(flag){            str = this.getString(info) ;            if(str.matches("\\d+")){                temp = Integer.parseInt(str) ;                flag = false ;    // 更改標誌位,將退出迴圈            }else{                System.out.println(err) ;            }        }        return temp ;    }};

public class Operate{    private Student stu[] = {new Student(1,"張三",0),new Student(2,"李四",0),        new Student(3,"王五",0),new Student(4,"趙六",0)} ;// 侯選人資訊    private boolean flag = true ;    public Operate(){        this.printInfo() ;    // 先輸出候選人資訊        while(flag){            this.vote() ;    // 迴圈調用投票        }        this.printInfo() ;    // 輸出投票之後的侯選人資訊        this.getResult() ;    // 得到結果    }    private void getResult(){    // 得到最終的投票結果        java.util.Arrays.sort(this.stu) ;    // 排序        System.out.println("投票最終結果:" + this.stu[0].getName()+"同學,最後以"+this.stu[0].getVote()+"票當選班長!") ;    }    public void vote(){    // 此方法完成投票功能        InputData input = new InputData() ;    // 輸入資料        int num = input.getInt("請輸入班長侯選人代號(數字0結束):","此選票無效,請輸入正確的侯選人代號!") ;        switch(num){            case 0:{                this.flag = false ;    // 中斷迴圈                break ;            }            case 1:{                this.stu[0].setVote(this.stu[0].getVote() + 1) ;                break ;            }            case 2:{                this.stu[1].setVote(this.stu[1].getVote() + 1) ;                break ;            }            case 3:{                this.stu[2].setVote(this.stu[2].getVote() + 1) ;                break ;            }            case 4:{                this.stu[3].setVote(this.stu[3].getVote() + 1) ;                break ;            }            default:{                System.out.println("此選票無效,請輸入正確的候選人代號!") ;            }        }    }    public void printInfo(){            for(int i=0;i<stu.length;i++){            System.out.println(this.stu[i].getStuNo() + ":"                + this.stu[i].getName() + "【"+this.stu[i].getVote()+"】") ;            }    }};

public class ExecDemo{    public static void main(String args[]){        new Operate() ;    }};

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.