如何解决Java中的“循环播放和候选投票”错误

时间:2019-06-19 23:44:02

标签: java oop voting

我正在制作投票系统,并希望投票系统能够循环播放,直到用户输入0并输出胜出的候选人为止

两个班

 package javaexamcode; 
   import java.util.Scanner;

   public class JavaExamCode {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Nominee mickey = new Nominee("Mickey Mouse");
        Nominee donald = new Nominee("Donald Duck");
        Nominee minnie = new Nominee("Minnie Mouse");
        Nominee goofy = new Nominee("Goofy");

        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.println();
            System.out.println("The presidential nominees are:");
            System.out.println("1. Mickey Mouse");
            System.out.println("2. Donald Duck");
            System.out.println("3. Minnie Mouse");
            System.out.println("4. Goofy");
            System.out.print("What is your vote? ");

            try{

                int vote=in.nextInt();

                if (vote==0){
                    break;
                }else if (vote == 1){
                    mickey.increaseVote();
                }else if (vote == 2){
                    donald.increaseVote();
                }else if (vote == 3){
                    minnie.increaseVote();
                }else if (vote ==4){
                    goofy.increaseVote();
                }
                else {
                    System.out.println("invalid");
                }
                break;
            } catch (Exception e){
                System.out.println("Invalid entry, try again");
                continue;

            }

        }
        Nominee[] candidates = new Nominee[]{mickey,donald,minnie,goofy};
        Nominee president = winner(candidates);

        System.out.println("The winner is " + president.toString() + " with " + president.totalVotes() + " votes.");
    }

    public static Nominee winner(Nominee[] all){
        Nominee most = all[0];

        for (int i = 1; i < all.length - 1; i++){
            if (all[i].totalVotes() > most.totalVotes()){
                most = all[i];
            }else if (all[i].totalVotes() == most.totalVotes()){
                String newName = all[i].toString() + " " + most.toString();
                most = new Nominee(newName, (all[i].totalVotes() + most.totalVotes()));
            }
        }

        return most;
    }

}






package javaexamcode; 
public class Nominee{
    private String name;
    private int votes;

    public Nominee(String name){
        this.name = name;
    }

    public Nominee (String name, int votes){
        this.name = name;
        this.votes = votes;
    }

    public void increaseVote(){
        votes++;
    }

    public String toString(){
        return name;
    }

    public int totalVotes(){
        return votes; 
    }

}

当您输入4时也会出现错误,然后输出获胜者是米奇和米妮,票数为0。 帮助将不胜感激

我已经在线搜索了,但是找不到很多

1 个答案:

答案 0 :(得分:0)

使用您的方法:

public static Nominee winner(Nominee[] all){
    Nominee most = all[0];

    for (int i = 1; i < all.length - 1; i++){
        if (all[i].totalVotes() > most.totalVotes()){
            most = all[i];
        }else if (all[i].totalVotes() == most.totalVotes()){
            String newName = all[i].toString() + " " + most.toString();
            most = new Nominee(newName, (all[i].totalVotes() + most.totalVotes()));
        }
    }

    return most;
}

您的for循环忽略了最后一个候选者(本例中为#4) 除了需要循环i < all.length - 1之外,还需要循环到i < all.length