请教高手一个题目: 
创建两个类Vote和Candidate。Vote类包括统计选票的数据域count,以及读取和处理选票的getCount、setCount、clear、increment和decrement方法,clear方法将票数count置为0,increment和decrement方法增加和减少票数。Candidate类具有数据域name(代表的姓名)、vote(记录代表接收的选票)和numberOfCandidates(记录代表的总数),以及读取name、vote和numberOfCandidates的getName、getVote和getNumberOfCandidates方法。
编写测试程序,统计竞选学生会主席的两个代表的选票。选票从键盘输入,数字1是代表1的选票,数字2代表2的选票。数字-1扣除代表1的一张选票,-2扣除代表2的一张选票,数字0表示统计结束。
谢谢指点!!!

解决方案 »

  1.   


    public class Vote {
    private int count; public Vote(){
    count=0;
    }
    public int getCount() {
    return count;
    }
    public void setCount(int count) {
    if(count<0)
    this.count --;
    else 
    this.count++;
    }
    //将票数至0
    public void clear(){
    count=0;
    }
    //票数加
    }import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class Candidate {
    private String name;
    private Vote vote;
    private int numberOfCandidates;
    public Candidate(String name){
    this.name=name;
    vote=new Vote();
    }
    public String getName() {
    return name;
    }
    public int getNumberOfCandidates() {
    numberOfCandidates=vote.getCount();
    return numberOfCandidates;
    }
    public Vote getVote() {
    return vote;
    }
    public void setVote(int voteNum) {
     vote.setCount(voteNum);
    }
    public static void main(String args[]){
    Candidate candidate1=new Candidate("Candidate 1");
    Candidate candidate2=new Candidate ("Candidate 2");
    voieStart(candidate1,candidate2);
    }
    public static void voieStart(Candidate candidate1,Candidate candidate2) {
    String input;
    int vote;
    while(true){
    try {
    input=new BufferedReader(new InputStreamReader(System.in)).readLine();
    vote=Integer.parseInt(input);
    switch(Math.abs(vote)){
    case 1: candidate1.setVote(vote); break;
    case 2: candidate2.setVote(vote);break;
    case 0 :System.out.println(candidate1.getName()+"get "+candidate1.getNumberOfCandidates());
    System.out.println(candidate2.getName()+"get "+candidate2.getNumberOfCandidates());
    return;
    default :
    System.out.println("error input!!!");
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }是你要的吗?
      

  2.   


    我也写了个,不知合意否?import javax.swing.JOptionPane;/**
     * 
     *创建两个类Vote和Candidate。
     *Vote类包括统计选票的数据域count,以及读取和处理选票的
     *getCount、setCount、 clear、increment和decrement方法,
     *clear方法将票数count置为0,increment和decrement方法增加和减少票数。
     *
     *Candidate类具有数据域name(代表的姓名)、vote(记录代表接收的选票)和
     *numberOfCandidates(记录代表的总数),以及读取name、vote和numberOfCandidates的
     *getName、getVote和 getNumberOfCandidates方法。
     *
     *编写测试程序,统计竞选学生会主席的两个代表的选票。
     *选票从键盘输入,数字1是代表1的选票,数字2代表2的选票。
     *数字-1扣除代表1的一张选票,-2扣除代表2的一张选票,数字0表示统计结束。
     *
     */public class Vote {

    private int count=0;

    int getCount(){
    return count;
    }

    void setCount(int count){
    this.count=count;
    }

    void clear(){
    count=0;
    }

    int increment(){
    return ++count;
    }

    int decrement(){
    return --count;
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Candidate no1 = new Candidate("no1");
    Candidate no2 = new Candidate("no2");
    int vote=5;
    String str="The two candidates are :"+no1.getName()+"\n"+no2.getName()+
    "\n Please press Yes to vote !";
    JOptionPane.showMessageDialog(null, str);
    while(vote!=0){
    str=JOptionPane.showInputDialog("Your vote is:");
    vote=Integer.valueOf(str);
    switch (vote) {
    case 1:
    no1.vote.increment();
    break;
    case -1:
    no1.vote.decrement();
    break;
    case 2:
    no2.vote.increment();
    break;
    case -2:
    no2.vote.decrement();
    break;
    default:
    break;
    }

    }
    str="\nThe result:\n"+"no1's vote:\n" +
    no1.getVote()+"\nno2's vote:\n"+no2.getVote();
    JOptionPane.showMessageDialog(null, str);

    }}class Candidate{
    private String name="";
    Vote vote=new Vote();
    static int numberOfCandidates=0;

    Candidate (String name){//构造函数
    this.name=name;
    numberOfCandidates++;
    }

    String getName(){
    return name;
    }

    int getVote(){
    return vote.getCount();
    }

    int getNumberOfCandidates(){
    return numberOfCandidates;
    }

    }