英文:
Is there a better way to store and retrieve the values of letters given by their position in the alphabet?
问题
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));
}
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;
}
}
英文:
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;
}
}
答案1
得分: 0
每个字符只是一个映射到整数值的关系,你可以打印它们的整数值,看到 A -> 65,通过进行简单的操作,比如
char d = 'D'; // D 只是 68
所以执行
d - 65 或者 d - 'A'
都会得到 3
你可以使用字符的整数值表示进行整数运算,像这样
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
char a = 'A';
System.out.println(a - 'A' + 1); // 返回 1
a = 'B';
System.out.println(a - 'A' + 1); // 返回 2
}
}
[更新] 对于你的特定情况,你只需要在替换之前将 ñ 替换为 O。
英文:
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
答案2
得分: -1
你可以使用Character.getNumericValue()。
这将为您提供字母的ASCII码。
由于该代码对于'a'不是1,您需要通过以下方式进行补偿:
int n = Caracter.getNumericValue(c) - Character.getNumericValue('a') + 1;
注意,这仅在您只有小写字符的情况下才有效。
一种与您的代码更接近的不同方法是简单地使用包含'a' ... 'z'的字符串,并使用indexOf(c)。
String alphabet = "abcdefg...z";
int n = alphabet.indexOf("a") + 1;
+1是必要的,因为索引从0开始。
英文:
You can use Character.getNumericValue().
This will give you the ASCII code for the letter.
As that code is not 1 for 'a' you need to compensate that by e.g.
int n = Caracter.getNumericValue(c) - Character.getNumericValue('a') + 1;
Note, that this will only work if you have only lower case characters.
A different approach, closer to your code, would be to simply use a string containing 'a' ... 'z' and use indexOf(c).
String alphabet = "abcdefg...z";
int n = alphabet.indexOf("a") + 1;
The +1 is necessary because index starts with 0.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论