如何修复 Java 中的 "looping and candidate voting" 错误

How to fix "looping and candidate voting" error in Java

我在做一个投票系统,想让投票系统一直循环,直到用户输入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票。 帮助将不胜感激

我在网上搜索过,但没找到多少

在你的方法中:

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 而不是循环条件 i < all.length - 1