【问题标题】:Convert a number-word (String) to its Integer Value将数字字(字符串)转换为其整数值
【发布时间】:2016-07-02 23:32:23
【问题描述】:

编辑:对,我忘了说明问题——事实上我得到了0 作为输出。

上下文

我的程序旨在获取用户输入的数字字(1-99)并将其输出为整数(即​​三十四 = 34)。我不知道代码中的错误在哪里,需要帮助:

Scanner scInput = new Scanner(System.in);
String word = scInput.nextLine(); //number in word-form (i.e. twenty six)

char[] charArray = word.toCharArray();//string to char array for word^
int divider = 0; //position of hyphen/space in charArray

所有 2 字数字均由一个十位值和一个个位值组成。假设语法正确 [english],连字符/空格 divider 之前的单词是十位,divider 之后的单词是个位。

阵列

//word values - components & syntax (1-99)

//ONES
public static final String[] wONES = {"one","two","three","four","five","six","seven","eight","nine"};

//TENS
public static final String[] wTENS = {null,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; 

//TEENS
public static final String[] wTEENS = {"ten", "eleven", "twelve", "thirteen","fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; 

我已将所有单词组件组织成 3 个不同的数组:ones、tens 和 teens。

//equivalent integer-array  of above String arrays

//ONES
public static final int[] nONES = {1,2,3,4,5,6,7,8,9};

//TENS
public static final int[] nTENS = {0,20,30,40,50,60,70,80,90};

//TEENS
public static final int[] nTEENS = {10,11,12,13,14,15,16,17,18,19};

我创建了 3 个与上述三个数组相同的其他数组,除了它们存储整数值。

代码

在这里,我将用户输入的字符串分成两部分:十位和个位。所以如果数字是 72:70 = 十,2 = 个。

int tensValue = 0; //number's tens value (i.e. 30)
int onesValue = 0; //ones value (i.e. 3)

        char[] tensArray = null; //array storing tens section of word (before divider)
        for (int u = 0; u < divider; u++){
            tensArray[u] = charArray[u];
        }
        String tens = new String(tensArray); //convert char array to String


        char[] onesArray = null; //array storing ones section of word (after divider)
        for (int u = divider + 1; u > divider && u < charArray.length; u++){
            onesArray[u] = charArray[u];
        }
        String ones = new String(onesArray);

        //searches for matches in String array for tens
        for(int u = 0; u < wTENS.length; u++){
            if(tens.equals(wTENS[u])){
                tensValue = nTENS[u];
                total += tensValue;

            }
        }

        //searches for matches in String array for ones
        for(int u = 0; u < wONES.length; u++){
            if(ones.equals(wONES[u])){
                onesValue = nONES[u];
                total += onesValue;

【问题讨论】:

  • 对于初学者来说,char[] tensArray = null; 应该类似于 char[] tensArray = new char[10];

标签: java arrays converter


【解决方案1】:

在您当前的代码中,您正在执行char[] tensArray = null;,这应该类似于char[] tensArray = new char[10];,否则您最终会得到NPE

这可能不是最有效的,但这里有一个简单且更好的方法来解决您的问题。

  1. 阅读该行并将其拆分为空白(假设您用空格分隔单词)。

  2. 在上述列表中搜索拆分后得到的每个标记,并将相应的数字(相同的索引)添加到您的答案中。

  3. 打印答案。

这里是sn-p的代码:

class Main
{
    public static final String[] wONES = {"one","two","three","four","five","six",
                                          "seven","eight","nine"};
    public static final String[] wTENS = {"ten","twenty","thirty","forty","fifty","sixty",
                                          "seventy","eighty","ninety"}; 
    public static final String[] wTEENS = {"eleven", "twelve", "thirteen","fourteen",
                                           "fifteen", "sixteen", "seventeen", "eighteen", 
                                           "nineteen"}; 

    public static final int[] nONES = {1,2,3,4,5,6,7,8,9};
    public static final int[] nTENS = {10,20,30,40,50,60,70,80,90};
    public static final int[] nTEENS = {11,12,13,14,15,16,17,18,19};

    public static void main (String[] args) throws Exception
    {
        Scanner scInput = new Scanner(System.in);
        String word = scInput.nextLine();
        int answer = 0;

        /* Assuming you are giving space between words */
        for(String s : word.split(" ")) {
            /* Scan wONES */
            for(int i = 0; i < wONES.length; i++) {
                if(wONES[i].equalsIgnoreCase(s)) {
                    answer += nONES[i];
                    continue;
                }
            }

            /* Scan wTENS */
            for(int i = 0; i < wTENS.length; i++) {
                if(wTENS[i].equalsIgnoreCase(s)) {
                    answer += nTENS[i];
                    continue;
                }
            }

            /* Scan wTEENS */
            for(int i = 0; i < wTEENS.length; i++) {
                if(wTEENS[i].equalsIgnoreCase(s)) {
                    answer += nTEENS[i];
                    continue;
                }
            }
        }

        System.out.println("Result: " + answer);
    }
}

输入:

thirty four

输出:

34

【讨论】:

    【解决方案2】:

    你有一个有趣的方法来解决这个问题。有几点需要改变:

    1. 我看不到您在哪里设置了分隔索引。

    2. 您似乎在字符数组方面做了很多工作,所以我猜您来自不同的语言。坚持使用字符串会很好。

    3. 您没有针对“青少年”。这看起来像是一个简单的疏忽。

    我在尝试保持原始方法的同时添加了这些修复:

    public static void main(String [] args) {
    
        Scanner scInput = new Scanner(System.in);
        String word = scInput.nextLine();
    
        int total = 0;
        int tensValue = 0; //number's tens value (i.e. 30)
        int onesValue = 0; //ones value (i.e. 3)
    
        int divider = word.indexOf('-');
        String tens = null;
        String ones = null;
        if (divider != -1) {
            tens = word.substring(0, divider);
            ones = word.substring(divider + 1);
        } else {
            ones = word;
        }
    
        //searches for matches in String array for tens
        if (tens != null) {
            for (int u = 0; u < wTENS.length; u++) {
                if (tens.equals(wTENS[u])) {
                    tensValue = nTENS[u];
                    total += tensValue;
                }
            }
        }
    
        //searches for matches in String array for ones
        for(int u = 0; u < wONES.length; u++) {
            if (ones.equals(wONES[u])) {
                onesValue = nONES[u];
                total += onesValue;
            }
        }
    
        // if a "teen" override what's in total
        for(int u = 0; u < wTEENS.length; u++) {
         if (ones.equals(wTEENS[u])) {
             total = nTEENS[u];
         }
        }
    
        System.out.println(total);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 2021-12-14
      • 1970-01-01
      • 2013-02-08
      • 1970-01-01
      • 2013-11-12
      相关资源
      最近更新 更多