【问题标题】:Voting system java投票系统java
【发布时间】:2014-12-03 14:03:33
【问题描述】:

我正在尝试在 Java 中创建一个投票系统,在其中输入候选人的姓名和他们获得的票数,然后我希望能够输出最高票数以及该候选人的姓名。到目前为止,我所拥有的是一种收集姓名和票数的主要方法。它将这些信息放入两个数组中。一个字符串数组用于名称,一个 int 数组用于投票数。我可以使用返回 int 数组中最大数字的 value 方法来计算最高票数。然后我打印返回的值没有任何问题,但我也希望能够从字符串数组中打印出获胜者的姓名,所以我想知道有什么方法可以将字符串数组中的信息引用到 int大批。我需要使用两个单独的数组来完成程序。这就是我目前所拥有的

import java.util.Scanner;
public class VotingCounter1
{
    public static void main(String [] args){
        Scanner userInput = new Scanner(System.in);
        final int SIZE = 6;
        int[] votes = new int[SIZE];
        String[] names = new String[SIZE];

        for (int i = 0; i < names.length && i < votes.length; i++){
            System.out.print("Enter candidate's name: ");
            names[i] = userInput.next( );
            System.out.print("Enter number of votes: ");
            votes[i] = userInput.nextInt( );
        }

        System.out.println("And the Winner is: " + highest(votes));
    }
    public static int highest(int[] votes){
        int high = votes[0];

        for (int i = 1; i < votes.length; i++){
            if (votes[i] > high){
                high = votes[i];
            }
        }
        return high;
    }
}

【问题讨论】:

    标签: java arrays methods counter voting


    【解决方案1】:

    投票和候选人的索引相同,因为您添加到相同的循环中。票数最高的候选人是获得最高票的候选人

          System.out.println("And the Winner is: " + highest(votes,names));
    
    
         public static String highest(int[] votes,String names[]){
        int high = votes[0];
        String s= names[0];
        for (int i = 1; i < votes.length; i++){
            if (votes[i] > high){
                high = votes[i];
                s=names[I];
            }
        }
        s=s+""+high;
        return s;
    } 
    

    【讨论】:

      【解决方案2】:

      最高票数的索引与候选人姓名的索引相同,因为您使用i 在同一个循环中添加了它们。所以得到票数最高的索引,就可以得到对应候选人的名字。

      【讨论】:

        【解决方案3】:

        以下将为您提供得票最高的人的姓名。

        names[Arrays.asList(names).indexOf(highest(votes))];
        

        请记住,这将找到最高(投票)的第一个实例。 IE。如果两个人都有50票,这也是最大票数。将找到带票的第一个名字。

        或者,使用具有 name 和 votes 属性的对象。更好的设计

        【讨论】:

          【解决方案4】:
          int pointer; // a class variable
          

          公共静态int最高(int []票){ int high = votes[0];

              for (int i = 1; i < votes.length; i++){
                  if (votes[i] > high){
                      high = votes[i];
                     pointer = i;
                  }
              }
              return high;
          

          现在您可以在打印时使用指针来使用名称数组的索引。

          System.out.println("And the Winner is: " +names[pointer]+"with"+ highest(votes)+"votes");
          

          【讨论】:

            【解决方案5】:
              System.out.println("And the Winner is: " + highest(votes,names));
            
                 public static String highest(int[] votes,String names[]){
                int high = votes[0];
                String s= names[0];
                for (int i = 1; i < votes.length; i++){
                    if (votes[i] > high){
                        high = votes[i];
                        s=names[I];
                    }
                }
                s=s+""+high;
                return s;
            } 
            

            【讨论】:

            • 这与本杰明上面给出的答案基本相同。更糟糕的是,尾随的 '}' 和前导 System.out.println 不在代码框中
            • 另外,请避免发布纯代码答案。如果您想帮助人们,请在您的代码中提供深入的解释。这样你也可以改进本杰明的答案。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-01-08
            • 2011-07-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多