【问题标题】:Jcreator Voting ProgramJcreator 投票计划
【发布时间】:2018-06-23 13:36:31
【问题描述】:

我的老师希望我们制作一个程序,计算来自可变数量选区的两名候选人的总票数。因此,用户将候选人的姓名作为字符串输入,然后在输入每个选区的选票之前被提示输入选区的数量。我遇到的麻烦是我必须使用一个数组来保持每个选区的投票总数。然后,在完成所有这些之后,我必须在每个选区完成后为每个候选人保留一个运行总票数,以及目前谁领先以及领先多少。我已经开始了我的程序,但老实说,我只是不知道从哪里开始,而且我知道到目前为止我的数组中的内容是不正确的。

import java.util.*;

public class Voting
{
    public static void main (String [] args)
    {
        Scanner scan = new Scanner(System.in);

        int rerun;
        int rerun2;

        while (rerun == 1)
        {
            System.out.print("Name of candidate 1: ");
            candidate1 = scan.next();
            System.out.print("Name of candidate 2: ");
            candidate2 = scan.next();

            System.out.print("\nPlease enter amount of precincts: ");
            presincts = scan.nextInt();

            System.out.print("\n Please enter amount of votes for     candidate 1: ");
        votes1 = scan.nextInt();

            System.out.print("\n Please enter amount of votes for candidate 2: ");
            votes2 = scan.nextInt();

            while (rerun2 == 1 && precincts >= 0 && votes1 >= 0 && votes2 >= 0)
            {
                int[] votes1 = new int[precincts];

                for(int i = 0; i < votes1.length; i++)
                {
                    votes1[i] = int [i];
                    System.out.println ("\n" + votes1);
                }

                int[] votes2 = new int[precincts];


                for(int i = 0; i < votes2.length; i++)
                {
                    votes2[i] = int [i];
                    System.out.println ("\n" + votes2);
                }
            }
        }
    }
}

【问题讨论】:

    标签: java arrays voting jcreator


    【解决方案1】:

    我不知道 rerun 的功能是什么,所以我把它排除在这个答案之外。

    您要做的第一件事是在课程开始时初始化变量:

    String candidate1;
    String candidate2;
    int precincts;
    int vote1;
    int vote2;
    int total1;
    int total2;
    int[] votes1;
    int[] votes2;
    

    请注意,您在 precincts 中有错字

    然后你需要得到候选人的名字和选区的数量(你已经正确地做到了):

    Scanner scan = new Scanner(System.in);
    
    System.out.print("Name of candidate 1: ");
    candidate1 = scan.next();
    System.out.print("Name of candidate 2: ");
    candidate2 = scan.next();
    
    System.out.print("\nPlease enter amount of precincts: ");
    precincts = scan.nextInt();
    

    然后您需要遍历选区的数量,并从每个选区获取每个候选人的票数。您可以通过初始化一个空数组来保存投票(例如,选区 1 中第一个候选人的投票将存储在votes1[0])。此外,保持运行总数的最简单方法是使用单独的变量 total1total2

    total1 = 0;
    total2 = 0;
    votes1 = new int[precincts];
    votes2 = new int[precincts];
    for (int i = 0; i < precincts; i++) {
        System.out.print("\nPlease enter amount of votes for candidate 1 in precinct " + (i + 1) + ": ");
        vote1 = scan.nextInt();
        votes1[i] = vote1;
        total1 = total1 + vote1;
    
        System.out.print("\nPlease enter amount of votes for candidate 2 in precinct " + (i + 1) + ": ");
        vote2 = scan.nextInt();
        votes2[i] = vote2;
        total2 = total2 + vote2;
    }
    

    for 循环中,您可以执行诸如打印候选人当前总票数之类的操作:

    System.out.println(candidate1 + " has " + total1 + " votes in total");
    

    并打印出谁是当前的领导者以及获得多少票:

    if (total1 > total2) {
        System.out.println(candidate1 + " is winning by " + (total1-total2) + " votes");
    } else {
        System.out.println(candidate2 + " is winning by " + (total2-total1) + " votes");
    }
    

    【讨论】:

    • 非常感谢您的帮助,这对帮助我更好地了解数组的工作原理非常有帮助!我什至能够操纵您编写的内容并让程序执行其他功能,这真的很棒。非常感谢! @fralewsmi
    • 没问题,如果您对答案感到满意,请单击接受按钮。干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多