What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Java

Java
Author: Message:
kezz
Full Member
***

Avatar
'kezz.,

Posts: 231
Reputation: 6
32 / Male / Flag
Joined: Sep 2006
O.P. Java
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);



}
}



This post was edited on 05-01-2008 at 07:28 AM by kezz.
[Image: 2764.png]
[Image: internet.png]
05-01-2008 07:28 AM
Profile E-Mail PM Web Find Quote Report
andrewdodd13
Senior Member
****

Avatar
Oh so retro

Posts: 870
Reputation: 16
34 / Male / Flag
Joined: Jan 2005
RE: Java
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
[Image: AndrewsStyle.png]
05-01-2008 08:11 AM
Profile E-Mail PM Web Find Quote Report
kezz
Full Member
***

Avatar
'kezz.,

Posts: 231
Reputation: 6
32 / Male / Flag
Joined: Sep 2006
O.P. RE: Java
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.
[Image: 2764.png]
[Image: internet.png]
05-01-2008 08:44 AM
Profile E-Mail PM Web Find Quote Report
RaceProUK
Elite Member
*****

Avatar

Posts: 6073
Reputation: 57
39 / Male / Flag
Joined: Oct 2003
RE: Java
* RaceProUK 'runs' code in head...

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

Damn case sensitivity!
[Image: spartaafk.png]
05-01-2008 04:02 PM
Profile PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On