嗨,我正在上这堂课的家庭作业,我遇到的问题是我对解决方案的思考过多

时间:2019-10-07 14:41:08

标签: java

我必须创建一个程序来转换输入字符串,例如“ 1onetwo34six5”的输出应为“ one12threefour6five” 如何编写此代码,我知道如何将数字转换为单词

static String digitToWord(char ch) {
    switch(ch) {    
    case '0': return "Zero";
    case '1': return "One";
    case '2': return "Two";
    case '3': return "Three";
    case '4': return "Four";
    case '5': return "Five";
    case '6': return "Six";
    case '7': return "Seven";
    case '8': return "Eight";
    case '9': return "Nine";
    }
    return "Unknown (" + ch + ")";
}

4 个答案:

答案 0 :(得分:0)

您需要增强用于首先解析传入字符串的逻辑:确定是需要将数字转换为字符串还是将字符串转换为数字。

对于第二种情况,更复杂的是您需要了解数字单词的限制在哪里。例如,如何知道一个数字字符串以“ onetwo”结尾?

对于您发布的特定部分,最好在映射中存储从数字到字符串的映射。切换更容易出错,以后也更难支持。

答案 1 :(得分:0)

我不知道这是否是最佳答案,但是您可以执行以下操作:

String Converter(String stringToConvert){
    stringToConvert = stringToConvert.replace("1","One");
    stringToConvert = stringToConvert.replace("2","Two");
    stringToConvert = stringToConvert.replace("3","Three");
    stringToConvert = stringToConvert.replace("4","Four");
    stringToConvert = stringToConvert.replace("5","Five");
    stringToConvert = stringToConvert.replace("6","Six");
    stringToConvert = stringToConvert.replace("7","Seven");
    stringToConvert = stringToConvert.replace("8","Eight");
    stringToConvert = stringToConvert.replace("9","Nine");
    stringToConvert = stringToConvert.replace("one","1");
    stringToConvert = stringToConvert.replace("two","2");
    stringToConvert = stringToConvert.replace("three","3");
    stringToConvert = stringToConvert.replace("four","4");
    stringToConvert = stringToConvert.replace("five","5");
    stringToConvert = stringToConvert.replace("six","6");
    stringToConvert = stringToConvert.replace("seven","7");
    stringToConvert = stringToConvert.replace("eight","8");
    stringToConvert = stringToConvert.replace("nine","9");
    stringToConvert = stringToConvert.toLowerCase();
    return stringToConvert;
}

注意,我在末尾使用toLowerCase来保持格式不变

答案 2 :(得分:0)

如果数据原则上是清晰的,则算法优先。

String convert(String text) {
    ...
    String result = "";
    int i = 0;
    while (i < text.length()) {
        int foundAndTranslated = findTranslation(text.substring(i));
        result += translated(foundAndTranslated);
        i += foundLength(foundAndTranslated);
    }
    return result;
}

在字符串中的某个点上,您正在寻找“ 1”或“一个”,并希望将其替换为“一个”或“ 1”。以上反映了这一点。

这向我强烈建议,保留char {"1", "one"}{"one", "1"}而不是char'1'而是简化字符串“ 1”。

然后应该考虑像“ y”这样的垃圾,其长度为1才能继续,从而导致“ y”或“?”。

答案 3 :(得分:0)

如果您只处理从“ 0” “ 9” 的字符串数字和从“零” 到< strong>“九” ,那么您可以执行的一种方法是将输入字符串分割成各自的元素。

由于字符串数字(1至9)和数字名称(0-9)在表示上是唯一的,因此可以使用二维(2D)字符串数组(或您喜欢的任何收集方法)来完成,列出接口和几个 for 循环,例如:

String[][] numbers = {{"one","1"}, {"two", "2"}, {"three","3"},
                      {"four", "4"}, {"five", "5"}, {"six","6"},
                      {"seven", "7"}, {"eight", "8"}, {"nine", "9"},
                      {"zero", "0"}};

String strg = "1onetwo34six5";
System.out.println("Original String: " + strg);

// Split the input string into String digits and numerical Names
String tmp = "";
List<String> splitList = new ArrayList<>();
for (int i = 0; i < strg.length(); i++) {
    String s = strg.substring(i, i+1);
    // Is the character a digit (0 to 9)?
    if (s.matches("\\d")) {
        // Yes it is...
        splitList.add(s);   // Add it to the splitList.
    }
    // Nope...it must be a numerical name.
    else {
        tmp+= s;   // Start creating the numerical name.
        /* Is the numerical name created thus far contained
           within the numbers 2D String Array?  */
        for (int j = 0; j < numbers.length; j++) {
            if (tmp.toLowerCase().equals(numbers[j][0])) {
                // Yes...It's here. Add it to the splitList
                splitList.add(tmp);
                tmp = "";  // Clear tmp in prep for the next numerical word.
                break;     // Get out of loop. No longer needed for this word.
            }
        }
        // Nope...get the next character.
    }
}

一旦获取了 split ,现在只需将输入字符串中的每个split元素与2D String Array中包含的元素进行比较,并应用适当的元素字符串值即可,这很简单:

/* The splitList List now contains what we need.
   It's just a matter of converting each element
   to its' represented value which we hold in the 
   numbers String Array and build a string from it. */
StringBuilder sb = new StringBuilder();
for (String element : splitList) {
    // Is it a numerical digit?
    if (element.matches("\\d")) {
        //Yes...it is so append its' respective numerical name to sb.
        for (int i = 0; i < numbers.length; i++) {
            if (element.equals(numbers[i][1])) {
                sb.append(numbers[i][0]);
            }
        }
    }
    // Nope...it must be a numerical Name.
    else {
        // Append its' respective numerical digit to sb.
        for (int i = 0; i < numbers.length; i++) {
            if (element.toLowerCase().equals(numbers[i][0])) {
                sb.append(numbers[i][1]);
            }
        }
    }
}
// Display the modified input string.
System.out.println("Modified String: " + sb.toString());

如果您想处理输入字符串中的空格,那么我认为您可以轻松找出此处需要什么并进行必要的修改。