Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Java.Challenges;
- import java.util.*;
- public class IowaCaucus {
- public static void main (String [] args) {
- Scanner input = new Scanner(System.in);
- System.out.println("Please enter the amount of people voting today: ");
- int voterNum = input.nextInt();
- int aliceNum = 0;//number of votes per candidate variable
- int bobNum = 0;//number of votes per candidate variable
- int charlieNum= 0;//number of votes per candidate variable
- System.out.println ("The three candidates are: Bob, Alice, and Charlie. Please rank the three candidates:");
- for(int i = 0; i < voterNum; i++) { // depending on how many people are voting is how many times the computer will ask the user to rank
- System.out.println("Voter Number " + (i + 1) + ":");
- System.out.println("Rank 1: ");
- String userVote = input.next();
- if (userVote.equals("Alice")){
- aliceNum = aliceNum + 3;// tallies the amount of points per ranking
- }else if (userVote.equals("Bob")){
- bobNum = bobNum + 3; // for the candidate ranked number 1 will gain 3 points
- }else {
- charlieNum = charlieNum + 3;
- }
- System.out.println("Rank 2: "); // for the candidate ranked number 2 will gain 2 points
- userVote = input.next();
- if(userVote.equals("Alice")) {
- aliceNum = aliceNum + 2;
- }else if(userVote.equals("Bob")) {
- bobNum = bobNum + 2;
- }else{
- charlieNum = charlieNum + 2;
- }
- System.out.println("Rank 3: ");// for the candidate ranked number 3 will gain 1 point
- userVote = input.next();
- if (userVote.equals("Alice")) {
- aliceNum = aliceNum + 1;
- }else if(userVote.equals("Bob")) {
- bobNum = bobNum + 1;
- }else{
- charlieNum = charlieNum + 1;
- }
- }
- System.out.println("The winner is of this election is : ");
- if(aliceNum > bobNum && aliceNum > charlieNum){ // if alice's points are greater than the other two then computer states she won
- System.out.println("Charlie");
- }
- else if(bobNum > aliceNum && bobNum > charlieNum){
- System.out.println("Bob");
- }
- else if (charlieNum > aliceNum && charlieNum > bobNum){
- System.out.println("Alice");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement