imnottellingyou
imnottellingyou

Reputation: 1

Jcreator Voting Program

My teacher wants us to make a program that counts the total number of votes for two candidates from a variable number of precincts. So the user inputs the candidates’ names as strings and is then prompted to enter the number of precincts prior to entering the votes for each precinct. What I am having trouble with is that I have to use an array to keep each precinct's vote total. Then after all of that is done I have to keep a running total of the votes for each candidate after each precinct is completed and who is currently leading and by how much. I have already begun my program but I am honestly just lost as to where to go from here, and I know that what I have in my arrays so far is not correct.

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);
                }
            }
        }
    }
}

Upvotes: -1

Views: 1605

Answers (1)

fralewsmi
fralewsmi

Reputation: 92

I don't know what the function of rerun is so I've left it out of this answer.

First thing you're going to want to do is initialize your variables at the start of your class:

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

Note you had a typo in precincts

Then you need to get the name of the candidates and the number of precincts (you've done this correctly already):

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();

Then you need to iterate through the number of precincts, and get the number of votes for each candidate from each precinct. You can do this by initializing an empty array which will keep the votes stored (eg, the votes for the first candidate in precinct 1 will be stored in votes1[0]). Additionally, the easiest way to keep a running total is to use a separate variable total1 and total2:

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;
}

From inside the for loop you can do things like print the current total votes for a candidate:

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

And print out who is the current leader and by how many votes:

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

Upvotes: 0

Related Questions