java - Is there a better way to store and retrieve the values of letters given by their position in the alphabet?
问题描述
I've got an assignment to write a program that adds the digits of an integer and the letters in a word (the value of each letter is given by its position in the alphabet, e.g. j = 10). After a while of trying to figure out a way to store and retrieve the value of each letter I decided to use a HashMap... but I wonder if there are better ways to do it (I'm sure there are)... so I'd like to get some suggestions on how to improve the code and learn some new things... thanks!
import java.util.HashMap;
import java.util.Scanner;
public class NLS {
public static HashMap<Character, Integer> alphabet;
static {
alphabet = new HashMap<>();
alphabet.put('a', 1); alphabet.put('b', 2); alphabet.put('c', 3); alphabet.put('d', 4);
alphabet.put('e', 5); alphabet.put('f', 6); alphabet.put('g', 7); alphabet.put('h', 8);
alphabet.put('i', 9); alphabet.put('j', 10); alphabet.put('k', 11); alphabet.put('l', 12);
alphabet.put('m', 13); alphabet.put('n', 14); alphabet.put('ñ', 15); alphabet.put('o', 16);
alphabet.put('p', 17); alphabet.put('q', 18); alphabet.put('r', 19); alphabet.put('s', 20);
alphabet.put('t', 21); alphabet.put('u', 22); alphabet.put('v', 23); alphabet.put('w', 24);
alphabet.put('x', 25); alphabet.put('y', 26); alphabet.put('z', 27);
}
public static void main(String[] args) {
char keepGoing;
do {
int sum;
Scanner scan = new Scanner(System.in);
System.out.println("\n\n\n____________________________________________________________");
System.out.println("\n\tEnter a number [integer] or a word/sentence [no spaces]:\n");
String input = scan.nextLine();
boolean numeric = numberOrWord(input);
if (numeric) {
sum = digitSum(Integer.parseInt(input));
System.out.println("Result= " + sum);
} else {
char letter = calculateLetter(input);
System.out.println("Result= " + letter);
}
System.out.println("<<PRESS s/S TO CONTINUE>>");
keepGoing = scan.next().charAt(0);
} while (keepGoing == 's'||keepGoing == 'S');
}
public static boolean numberOrWord (String str){
try{
Double.parseDouble(str);
return true;
}catch(NumberFormatException e){
return false;
}
}
public static int digitSum(int number){
int sum = 0;
while(number > 0){
sum = sum + number % 10;
number /= 10;
}
return sum;
}
public static char calculateLetter(String word){
int sumPV = 0;
for(char digit : word.toCharArray()){
sumPV += alphabet.get(Character.toLowerCase(digit));
}
/* for(int i = 0, l = word.length(); i < l; i++){
char c = word.toLowerCase().charAt(i);
sumPV = sumPV + alphabet.get(c);
}*/
while(sumPV > 27){
sumPV =digitSum(sumPV);
}
char letter = (char) 0;
for(char key : alphabet.keySet()){
if(alphabet.get(key).equals(sumPV)) {
letter = key;
}
}
return letter;
}
}
解决方案
Every char is just a mapping to integer value and you can print their intger values to see that A -> 65 by doing simple operations like
char d = 'D'; // D is just 68
so doing
d - 65 Or d - 'A'
Both result in 3
You can use the integer value representations for char and do integer operations like so
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
char a = 'A';
System.out.println(a - 'A' + 1); //Returns 1
a = 'B';
System.out.println(a - 'A' + 1); //Returns 2
}
}
[Update] For your specific case you just need to replace ñ with O before replacing
推荐阅读
- android - 如何将ASCII转换为十六进制并使用蓝牙发送?
- jquery - 使用 Jquery 删除不匹配的元素
- spring - Spring security - 未调用提供的自定义身份验证
- python - 在 Python 中恢复以前的版本
- asp.net-core-2.0 - 如何使 dot.net 核心应用程序显示错误而不是发出空白的 500 错误页面
- javascript - 如何显示日期取决于从日期
- webpack - Webpack and VueJs CLI v3 – Need relative path for images and web-fonts
- ios - 使用 LaunchImages 和 LaunchScreen 的区别
- java - 使用javamail如何发送整数值0
- ios - 带有自定义 UIButton 的 UIBarButtonItem 在 iOS <= 10 上不可见