Shoutbox

Java - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Java (/showthread.php?tid=83473)

Java by kezz on 05-01-2008 at 07:28 AM

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



}
}



RE: Java by andrewdodd13 on 05-01-2008 at 08:11 AM

It looks like it could work, but since we don't have cs1.Keyboard we can't compile it to find out...

What's a Precint btw? :P


RE: Java by kezz on 05-01-2008 at 08:44 AM

thanks, thats all i need to know.
It's a long story. I'm a bit busy to explain now but thanks for the help.


RE: Java by RaceProUK on 05-01-2008 at 04:02 PM

* RaceProUK 'runs' code in head...

I pressed 'y' and it wouldn't let me add more votes.

Damn case sensitivity!