Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. package Java.Challenges;
  2. import java.util.*;
  3. public class IowaCaucus {
  4.  
  5. public static void main (String [] args) {
  6.  
  7. Scanner input = new Scanner(System.in);
  8. System.out.println("Please enter the amount of people voting today: ");
  9. int voterNum = input.nextInt();
  10. int aliceNum = 0;//number of votes per candidate variable
  11. int bobNum = 0;//number of votes per candidate variable
  12. int charlieNum= 0;//number of votes per candidate variable
  13.  
  14. System.out.println ("The three candidates are: Bob, Alice, and Charlie. Please rank the three candidates:");
  15.  
  16. 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
  17. System.out.println("Voter Number " + (i + 1) + ":");
  18. System.out.println("Rank 1: ");
  19. String userVote = input.next();
  20. if (userVote.equals("Alice")){
  21. aliceNum = aliceNum + 3;// tallies the amount of points per ranking
  22. }else if (userVote.equals("Bob")){
  23. bobNum = bobNum + 3; // for the candidate ranked number 1 will gain 3 points
  24. }else {
  25. charlieNum = charlieNum + 3;
  26. }
  27. System.out.println("Rank 2: "); // for the candidate ranked number 2 will gain 2 points
  28. userVote = input.next();
  29. if(userVote.equals("Alice")) {
  30. aliceNum = aliceNum + 2;
  31. }else if(userVote.equals("Bob")) {
  32. bobNum = bobNum + 2;
  33. }else{
  34. charlieNum = charlieNum + 2;
  35. }
  36. System.out.println("Rank 3: ");// for the candidate ranked number 3 will gain 1 point
  37. userVote = input.next();
  38. if (userVote.equals("Alice")) {
  39. aliceNum = aliceNum + 1;
  40. }else if(userVote.equals("Bob")) {
  41. bobNum = bobNum + 1;
  42. }else{
  43. charlieNum = charlieNum + 1;
  44. }
  45. }
  46.  
  47. System.out.println("The winner is of this election is : ");
  48. if(aliceNum > bobNum && aliceNum > charlieNum){ // if alice's points are greater than the other two then computer states she won
  49. System.out.println("Charlie");
  50. }
  51. else if(bobNum > aliceNum && bobNum > charlieNum){
  52. System.out.println("Bob");
  53. }
  54. else if (charlieNum > aliceNum && charlieNum > bobNum){
  55. System.out.println("Alice");
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement