简体   繁体   English

将字母从字符串转换为数字

[英]Converting Letters from String to Numbers

Hello I am new to java and I am having a hard time on converting the letters from strings to numbers for example, the user will input "I am Okay" and the output will become 901-13015-11-1-25 since, I = 9 space = 0 A = 1 M = 13 space = 0 O = 15 K = 11 A = 1 Y = 25. I tried using switch, but I am having a hard time, because the program will print what I input in the string, well here is the test code I created, I hope someone can help me or give me tips and advices.您好,我是 java 的新手,我很难将字母从字符串转换为数字,例如,用户将输入“我很好”,output 将变为 901-13015-11-1-25,因为,我= 9 空间 = 0 A = 1 M = 13 空间 = 0 O = 15 K = 11 A = 1 Y = 25。我尝试使用 switch,但我很难,因为程序会打印我在字符串,这里是我创建的测试代码,我希望有人可以帮助我或给我提示和建议。

import java.util.*;
public class New2
{
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Input the First string: ");
    String fs = input.nextLine();
    
    fstoInt(fs);
    System.out.println(fs);

}

public static int fstoInt(String fs) {
    int num = 0;
    switch (fs) {
    case "A":
        num = 1;
        break;
    case "B":
        num = 2;
        break;
    case "C":
        num = 3;
        break;
    case "D":
        num = 4;
        break;
    case "E":
        num = 5;
        break;
    case "F":
        num = 6;
        break;
    case "G":
        num = 7;
        break;
    }
    return num;
}
}

output will become 901-13015-11-1-25 since, I = 9 space = 0 A = 1 M = 13 space = 0 O = 15 K = 11 A = 1 Y = 25 output 将变为 901-13015-11-1-25 因为,I = 9 空格 = 0 A = 1 M = 13 空格 = 0 O = 15 K = 11 A = 1 Y = 25

I don't think you read the assignment properly.我认为您没有正确阅读作业。

That would mean that: AA becomes 11 .这意味着: AA变为11 And K ... also becomes 11 .并且K ... 也变为11 Not sure this is what you wanted.不确定这是你想要的。

switch (fs) {
    case "A":

assuming it's all consecutive, with "A" becoming 1, and "Z" becoming 26: characters are just numbers (their unicode value), and those letters are in sequence.假设它都是连续的,“A”变为 1,“Z”变为 26:字符只是数字(它们的 unicode 值),并且这些字母是按顺序排列的。 So, instead of a gigantic switch block, all you need is:因此,您需要的不是巨大的开关块,而是:

int num = fs.charAt(0) - 'A' + 1;

this will turn "A" into 1, and "Z" into 26.这会将“A”变为 1,将“Z”变为 26。

As the comments already said, your problem is that fs isn't just "A" .正如评论已经说过的那样,您的问题是fs不仅仅是"A" It's "Hello" ."Hello" You need to loop through the characters.您需要遍历字符。 As space gets special treatment (turning into 0), and presumably anything that isn't "A" - "Z" should crash:随着空间得到特殊处理(变成 0),并且可能任何不是“A”-“Z”的东西都应该崩溃:

for (char c : fs.toCharArray()) {
    int num = charToCode(c);
}

and then write charToCode:然后写charToCode:

public int charToCode(char c) throws IllegalArgumentException {
    if (c == ' ') return 0;
    if (c >= 'A' && c <= 'Z') return c - 'A' + 1;
    throw new IllegalArgumentException(
      "Only spaces and capital letters allowed");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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