# Election Java Problem

• November 13th, 2009, 03:30 PM
ronz
Election Java Problem
Hello Everyone.

I'm having difficulty solving this election problem.

Due 11/9/2009
The purpose of this project is to demonstrate knowledge of loops and counters.

You will present to the user a choice of at least 3 items to chose from. Choices can be repeatedly made until the user asks to exit. At the end the counts for each choice and the percentages will be displayed. A winner needs to be declared if there is a simple majority.

Feel free to use different candidates for a different election like SGA President, or Favorite Actor, or Favorite Video Game [ keep it clean please ].

Sample Run

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 1

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 3

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 3

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 2

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 3

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 1

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 2

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 3

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 3

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 1

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 1

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 2

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 3

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 3

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 1

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 44

Ooops! Invalid Candidate!

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 3

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 0

Results:

Corzine 5 31.3%
Christie 3 18.8%
Daggett 8 50.0%

Daggett declared winner
Press any key to continue . . .

Another Sample Run

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 2

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 3

1 - Jon Corzine(D)
2 - Chris Christie(R)
3 - Chris Daggett(I)

Enter the voter's choice (1-3, 0 Exit): 0

Results:

Corzine 0 0.0%
Christie 1 50.0%
Daggett 1 50.0%

No winner declared!
Press any key to continue . . .

Its not solved by using files and not a for loop.
Here are some examples I found in my textbook that relate to the problem.
Code :

```import java.util.Scanner;   public class DemoDoWhile { public static void main(String args[]) { int customerChoice;   Scanner input = new Scanner(System.in);   do { // display menu System.out.println("\nIgloo Ice Cream Store"); System.out.println("\nMenu Choices"); System.out.println("\n1 - Sundae"); System.out.println("2 - Shake"); System.out.println("3 - Cone"); System.out.println("\n0 - Exit\n");   System.out.print("Enter the customer's choice: "); customerChoice = input.nextInt();   switch (customerChoice) { case 0: System.out.println("\nBye bye!\n"); break; case 1: System.out.println("\nSundaes Use:"); System.out.println("\t8 oz. ice cream"); System.out.println("\t1 oz. chopped nuts"); System.out.println("\t2 oz. chocolate sauce"); System.out.println("\t1 cherry"); break; case 2: System.out.println("\nShakes Use:"); System.out.println("\t10 oz. ice cream"); System.out.println("\t1 oz. chocolate sauce"); break; case 3: System.out.println("\nCones Use:"); System.out.println("\t6 oz. ice cream"); System.out.println("\t1 cone"); break; default: System.out.println("\nyou entered an invalid choice"); } // end switch } while (customerChoice != 0); } // end main method } // end class   Example 2   import java.util.Random;   public class FlipCoinsLoop { public static void main(String args[]) { Random randomNumbers = new Random(); int numHeads = 0; int numTails = 0; int numFlips;   //simulate flipping a coin multiple times and tally what we have   for (numFlips = 1; numFlips <= 10; numFlips++) {     if (randomNumbers.nextBoolean()) { System.out.println("Heads"); numHeads++; } else { System.out.println("Tails"); numTails++; } }   System.out.printf("\nTally:\n"); System.out.printf("%s %d\n","Heads",numHeads); System.out.printf("%s %d\n","Tails",numTails); } }   ex. 3   public class IntDivisionCast { public static void main(String[] args)   { // declare variables int numberVotesBob, numberVotesPat, totalNumberVotes; double percentVotesBob, percentVotesPat;   // get data numberVotesBob = 145; numberVotesPat = 197;     // calc data totalNumberVotes = numberVotesBob + numberVotesPat;   // integer division avoided by cast (double) "pretends" that // numberVotesBob is a double for calculation   percentVotesBob = (double) numberVotesBob / totalNumberVotes * 100; percentVotesPat = (double) numberVotesPat / totalNumberVotes * 100;   // display results System.out.printf("\n%-12s %6.2f%%\n", "% Votes Bob",percentVotesBob); System.out.printf("\n%-12s %6.2f%%\n", "% Votes Pat",percentVotesPat);   } }```
If anyone can suggest direction or guidance for me, I would appreciate it. I was using files before to solve it and my professor said it was wrong.

Thanks for any help

ron