Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //a simple program that takes in a string of characters, and displays the scores of 5 players based off of that string.
- public class Tally {
- // create an array to store the scores of each of the five players. initialize
- // the scores to 0 for each person.
- private int friendScores[] = { 0, 0, 0, 0, 0 };
- // a function that selects a score increment or decrement based on the character
- // passed to it.
- public void selectAction(char initial) {
- switch (initial) {
- case 'a':
- friendScores[0]++;
- break;
- case 'b':
- friendScores[1]++;
- break;
- case 'c':
- friendScores[2]++;
- break;
- case 'd':
- friendScores[3]++;
- break;
- case 'e':
- friendScores[4]++;
- break;
- case 'A':
- friendScores[0]--;
- break;
- case 'B':
- friendScores[1]--;
- break;
- case 'C':
- friendScores[2]--;
- break;
- case 'D':
- friendScores[3]--;
- break;
- case 'E':
- friendScores[4]--;
- break;
- }
- }
- // count iterates through the entire string and will increment/decrement scores
- // based on the character chain read.
- public void count(String chain) {
- for (int i = 0; i < chain.length(); i++) {
- selectAction(chain.charAt(i));
- }
- }
- public void displayScores() {
- for (int i = 0; i < friendScores.length; i++)
- System.out.println("Person: " + (i + 1) + "'s score is: " + friendScores[i]);
- }
- public static void main(String[] args) {
- Tally tl = new Tally();
- tl.count("dbbaCEDbdAacCEAadcB");
- tl.displayScores();
- }
- }
Add Comment
Please, Sign In to add comment