Guest User

Untitled

a guest
Jul 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. //a simple program that takes in a string of characters, and displays the scores of 5 players based off of that string.
  2. public class Tally {
  3.  
  4. // create an array to store the scores of each of the five players. initialize
  5. // the scores to 0 for each person.
  6. private int friendScores[] = { 0, 0, 0, 0, 0 };
  7.  
  8. // a function that selects a score increment or decrement based on the character
  9. // passed to it.
  10. public void selectAction(char initial) {
  11. switch (initial) {
  12.  
  13. case 'a':
  14. friendScores[0]++;
  15. break;
  16. case 'b':
  17. friendScores[1]++;
  18. break;
  19. case 'c':
  20. friendScores[2]++;
  21. break;
  22. case 'd':
  23. friendScores[3]++;
  24. break;
  25. case 'e':
  26. friendScores[4]++;
  27. break;
  28. case 'A':
  29. friendScores[0]--;
  30. break;
  31. case 'B':
  32. friendScores[1]--;
  33. break;
  34. case 'C':
  35. friendScores[2]--;
  36. break;
  37.  
  38. case 'D':
  39. friendScores[3]--;
  40. break;
  41.  
  42. case 'E':
  43. friendScores[4]--;
  44. break;
  45.  
  46. }
  47. }
  48.  
  49. // count iterates through the entire string and will increment/decrement scores
  50. // based on the character chain read.
  51. public void count(String chain) {
  52. for (int i = 0; i < chain.length(); i++) {
  53. selectAction(chain.charAt(i));
  54. }
  55. }
  56.  
  57. public void displayScores() {
  58.  
  59. for (int i = 0; i < friendScores.length; i++)
  60.  
  61. System.out.println("Person: " + (i + 1) + "'s score is: " + friendScores[i]);
  62.  
  63. }
  64.  
  65. public static void main(String[] args) {
  66. Tally tl = new Tally();
  67. tl.count("dbbaCEDbdAacCEAadcB");
  68. tl.displayScores();
  69.  
  70. }
  71. }
Add Comment
Please, Sign In to add comment