简体   繁体   中英

Putting switch statement into loop java

I have the following code which scores the user-input string according to Scrabble scoring rules:

import java.util.Scanner;

public class ScrabbleScorer {

    public static void main(String[] args) {
        Scanner sc = new Scanner (System.in);
        System.out.print ("Enter string: ");
        String str = sc.nextLine();
        int score = computeScore (str);
        System.out.println ("The score for '" + str + "' is: " + score);
        sc.close();
    }

    public static int computeScore (String str) {
        str = str.toUpperCase();
        int score = 0;
        for(int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            switch(ch){
            case 'A': score = score + 1;break;
            case 'B': score = score + 3;break;
            case 'C': score = score + 3;break;
            case 'D': score = score + 2;break;
            case 'E': score = score + 1;break;
            case 'F': score = score + 4;break;
            case 'G': score = score + 2;break;
            case 'H': score = score + 4;break;
            case 'I': score = score + 1;break;
            case 'J': score = score + 8;break;
            case 'K': score = score + 5;break;
            case 'L': score = score + 1;break;
            case 'M': score = score + 3;break;
            case 'N': score = score + 1;break;
            case 'O': score = score + 1;break;
            case 'P': score = score + 3;break;
            case 'Q': score = score + 10;break;
            case 'R': score = score + 1;break;
            case 'S': score = score + 1;break;
            case 'T': score = score + 1;break;
            case 'U': score = score + 1;break;
            case 'V': score = score + 4;break;
            case 'W': score = score + 4;break;
            case 'X': score = score + 8;break;
            case 'Y': score = score + 4;break;
            case 'Z': score = score + 10;break;
            default : score = score;
            }

        }
        return score;
    }


}

I was wondering if there's a way to wrap the switch into a loop so that it doesn't have to be written out by hand 26 times. Thanks

I think you can use another, cleaner approach.

You could use a HashMap<Character, Integer> to store the character-point combination. Then you simply iterate over the string and increment your score:

HashMap<Character, Integer> pointScheme = new HashMap<>();
pointScheme.put('A', 1);
...
str = str.toUpper();
for(int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            score += pointScheme.get(ch);
}

This should result into cleaner code which is also easier to maintain.

Another way you can do:

public static int computeScore (String str) {
        //0-A, 1-B, 2-C, ....
        char[] scoresTable = {1, 3, 3, 2, 1, 4};

        str = str.toUpperCase();
        int score = 0;
        for(int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            score += scoresTable[ch - 'A'];
        }
        return score;
    }

you can use an array to store the scores. Each element is equivalent to each char from A to Z.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM