Can someone check this for me to make sure it will do what I want it to?
Program Description: Submits votes for a current election between two [nonreal] characters, Polly Tichen and Ernest Orator. [I didn't make the names. -.-]
The program first asks users to input votes for each precinct for the candidates. It asks if they want to add more. When they are done, it prints out the total votes, percentage of each candidate, and their total wins, losses, and ties of each precinct.
quote:
Originally posted by CODE
import cs1.Keyboard;
import java.text.NumberFormat;
public class Election
{
public static void main (String[] args)
{
// Initializations
int votesForPolly=0; // number of votes for Polly
int votesForErnest=0; // number of votes for Ernest
int totalPolly=0; // running total of votes for Polly
int totalErnest=0; // running total of votes for Ernest
double percentPolly;
double percentErnest;
int winPolly=0;
int winErnest=0;
int tie=0;
String response; // answer ('y' or 'n') to the "more precincts" question
// Loop to "process" the votes in each precinct
System.out.println ("Would you like to vote? (Y for yes, " +
"N for no)");
response = Keyboard.readString();
while (response.equals("Y")){
System.out.println ("Please enter any new votes for Polly Tichen.");
votesForPolly = Keyboard.readInt();
totalPolly += votesForPolly;
System.out.println ("Please enter any new votes for Ernest Orator.");
votesForErnest = Keyboard.readInt();
totalErnest += votesForErnest;
System.out.println ("Keep updating? (Y for yes, N for no)");
response = Keyboard.readString();
if (votesForPolly > votesForErnest)
winPolly++;
else if (votesForPolly < votesForErnest)
winErnest++;
else
tie++;
}
// Print out the results
percentPolly = (double) totalPolly/(totalPolly + totalErnest);
percentErnest = (double) totalErnest/(totalPolly + totalErnest);
System.out.println ();
System.out.println ("Polly Tichen has " + totalPolly + " votes.");
System.out.println ("Ernest Orator has " + totalErnest + " votes.");
if (totalPolly > totalErnest)
System.out.println ("Polly Tichen is winning!");
else if (totalPolly < totalErnest)
System.out.println ("Ernest Orator is winning!");
else
System.out.println ("It is a tie!");
NumberFormat percent = NumberFormat.getPercentInstance();
System.out.println ();
System.out.println ("Polly Tichen's percentage: "
+ percent.format(percentPolly));
System.out.println ("Ernest Orator's percentage: "
+ percent.format(percentErnest));
System.out.println ();
System.out.println ("Polly's Precint Wins: " + winPolly);
System.out.println ("Ernest's Precint Wins: " + winErnest);
System.out.println ("Precint Ties: " + tie);
}
}