<tfoot id='1irN3'></tfoot>

      <small id='1irN3'></small><noframes id='1irN3'>

        <bdo id='1irN3'></bdo><ul id='1irN3'></ul>
      <i id='1irN3'><tr id='1irN3'><dt id='1irN3'><q id='1irN3'><span id='1irN3'><b id='1irN3'><form id='1irN3'><ins id='1irN3'></ins><ul id='1irN3'></ul><sub id='1irN3'></sub></form><legend id='1irN3'></legend><bdo id='1irN3'><pre id='1irN3'><center id='1irN3'></center></pre></bdo></b><th id='1irN3'></th></span></q></dt></tr></i><div id='1irN3'><tfoot id='1irN3'></tfoot><dl id='1irN3'><fieldset id='1irN3'></fieldset></dl></div>

      1. <legend id='1irN3'><style id='1irN3'><dir id='1irN3'><q id='1irN3'></q></dir></style></legend>

        在 Java 中将单词转换为数字

        时间:2023-10-14
        • <bdo id='tR5tk'></bdo><ul id='tR5tk'></ul>
          <i id='tR5tk'><tr id='tR5tk'><dt id='tR5tk'><q id='tR5tk'><span id='tR5tk'><b id='tR5tk'><form id='tR5tk'><ins id='tR5tk'></ins><ul id='tR5tk'></ul><sub id='tR5tk'></sub></form><legend id='tR5tk'></legend><bdo id='tR5tk'><pre id='tR5tk'><center id='tR5tk'></center></pre></bdo></b><th id='tR5tk'></th></span></q></dt></tr></i><div id='tR5tk'><tfoot id='tR5tk'></tfoot><dl id='tR5tk'><fieldset id='tR5tk'></fieldset></dl></div>
            <tbody id='tR5tk'></tbody>

            • <small id='tR5tk'></small><noframes id='tR5tk'>

                <legend id='tR5tk'><style id='tR5tk'><dir id='tR5tk'><q id='tR5tk'></q></dir></style></legend>

              1. <tfoot id='tR5tk'></tfoot>
                  本文介绍了在 Java 中将单词转换为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我见过很多算法,你给它们一个数字,比如123".并将其转换为一百二十三.但我似乎找不到相反的东西,而且我找到的那些只能做到 1000 号.谁能以正确的方式指导我,因为我可以做些什么来创建一个采用"的方法.一千二百三十四"并回馈1234"

                  I have seen a lot of algorithms in which you give them a number say "123" and it converts it to One hundred twenty three. But I can't seem to find something that does the opposite, and the ones I did find only do it up to the number 1000. Can anyone direct me in the correct way as what I could do to create a method that takes "One thousand two hundred and thirty four" and gives back "1234"

                  推荐答案

                  我希望下面的代码在大多数情况下都能完成这项工作.但是,由于我尚未正确测试,可能需要进行一些修改.

                  I hope below code will do the job in most of the cases. However some modification might be required as I've not tested properly yet.

                  假设:

                  1. 不允许使用正、负、加、减.
                  2. Lac, crore 是不允许的.
                  3. 仅支持英语.

                  如果你需要支持前两点,你可以很容易地做到这一点.

                  If you need to support first two points, you can very easily do that.

                      boolean isValidInput = true;
                      long result = 0;
                      long finalResult = 0;
                      List<String> allowedStrings = Arrays.asList
                      (
                      "zero","one","two","three","four","five","six","seven",
                      "eight","nine","ten","eleven","twelve","thirteen","fourteen",
                      "fifteen","sixteen","seventeen","eighteen","nineteen","twenty",
                      "thirty","forty","fifty","sixty","seventy","eighty","ninety",
                      "hundred","thousand","million","billion","trillion"
                      );
                  
                      String input="One hundred two thousand and thirty four";
                  
                      if(input != null && input.length()> 0)
                      {
                          input = input.replaceAll("-", " ");
                          input = input.toLowerCase().replaceAll(" and", " ");
                          String[] splittedParts = input.trim().split("\s+");
                  
                          for(String str : splittedParts)
                          {
                              if(!allowedStrings.contains(str))
                              {
                                  isValidInput = false;
                                  System.out.println("Invalid word found : "+str);
                                  break;
                              }
                          }
                          if(isValidInput)
                          {
                              for(String str : splittedParts)
                              {
                                  if(str.equalsIgnoreCase("zero")) {
                                      result += 0;
                                  }
                                  else if(str.equalsIgnoreCase("one")) {
                                      result += 1;
                                  }
                                  else if(str.equalsIgnoreCase("two")) {
                                      result += 2;
                                  }
                                  else if(str.equalsIgnoreCase("three")) {
                                      result += 3;
                                  }
                                  else if(str.equalsIgnoreCase("four")) {
                                      result += 4;
                                  }
                                  else if(str.equalsIgnoreCase("five")) {
                                      result += 5;
                                  }
                                  else if(str.equalsIgnoreCase("six")) {
                                      result += 6;
                                  }
                                  else if(str.equalsIgnoreCase("seven")) {
                                      result += 7;
                                  }
                                  else if(str.equalsIgnoreCase("eight")) {
                                      result += 8;
                                  }
                                  else if(str.equalsIgnoreCase("nine")) {
                                      result += 9;
                                  }
                                  else if(str.equalsIgnoreCase("ten")) {
                                      result += 10;
                                  }
                                  else if(str.equalsIgnoreCase("eleven")) {
                                      result += 11;
                                  }
                                  else if(str.equalsIgnoreCase("twelve")) {
                                      result += 12;
                                  }
                                  else if(str.equalsIgnoreCase("thirteen")) {
                                      result += 13;
                                  }
                                  else if(str.equalsIgnoreCase("fourteen")) {
                                      result += 14;
                                  }
                                  else if(str.equalsIgnoreCase("fifteen")) {
                                      result += 15;
                                  }
                                  else if(str.equalsIgnoreCase("sixteen")) {
                                      result += 16;
                                  }
                                  else if(str.equalsIgnoreCase("seventeen")) {
                                      result += 17;
                                  }
                                  else if(str.equalsIgnoreCase("eighteen")) {
                                      result += 18;
                                  }
                                  else if(str.equalsIgnoreCase("nineteen")) {
                                      result += 19;
                                  }
                                  else if(str.equalsIgnoreCase("twenty")) {
                                      result += 20;
                                  }
                                  else if(str.equalsIgnoreCase("thirty")) {
                                      result += 30;
                                  }
                                  else if(str.equalsIgnoreCase("forty")) {
                                      result += 40;
                                  }
                                  else if(str.equalsIgnoreCase("fifty")) {
                                      result += 50;
                                  }
                                  else if(str.equalsIgnoreCase("sixty")) {
                                      result += 60;
                                  }
                                  else if(str.equalsIgnoreCase("seventy")) {
                                      result += 70;
                                  }
                                  else if(str.equalsIgnoreCase("eighty")) {
                                      result += 80;
                                  }
                                  else if(str.equalsIgnoreCase("ninety")) {
                                      result += 90;
                                  }
                                  else if(str.equalsIgnoreCase("hundred")) {
                                      result *= 100;
                                  }
                                  else if(str.equalsIgnoreCase("thousand")) {
                                      result *= 1000;
                                      finalResult += result;
                                      result=0;
                                  }
                                  else if(str.equalsIgnoreCase("million")) {
                                      result *= 1000000;
                                      finalResult += result;
                                      result=0;
                                  }
                                  else if(str.equalsIgnoreCase("billion")) {
                                      result *= 1000000000;
                                      finalResult += result;
                                      result=0;
                                  }
                                  else if(str.equalsIgnoreCase("trillion")) {
                                      result *= 1000000000000L;
                                      finalResult += result;
                                      result=0;
                                  }
                              }
                  
                              finalResult += result;
                              result=0;
                              System.out.println(finalResult);
                          }
                      }
                  

                  这篇关于在 Java 中将单词转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在Java中生成随机正数和负数 下一篇:解释 Integer.MAX_VALUE 和 Integer.MIN_VALUE 在数组中查找最小值和最大值

                  相关文章

                    1. <tfoot id='zDRlY'></tfoot>

                      <small id='zDRlY'></small><noframes id='zDRlY'>

                      <i id='zDRlY'><tr id='zDRlY'><dt id='zDRlY'><q id='zDRlY'><span id='zDRlY'><b id='zDRlY'><form id='zDRlY'><ins id='zDRlY'></ins><ul id='zDRlY'></ul><sub id='zDRlY'></sub></form><legend id='zDRlY'></legend><bdo id='zDRlY'><pre id='zDRlY'><center id='zDRlY'></center></pre></bdo></b><th id='zDRlY'></th></span></q></dt></tr></i><div id='zDRlY'><tfoot id='zDRlY'></tfoot><dl id='zDRlY'><fieldset id='zDRlY'></fieldset></dl></div>
                      <legend id='zDRlY'><style id='zDRlY'><dir id='zDRlY'><q id='zDRlY'></q></dir></style></legend>
                      • <bdo id='zDRlY'></bdo><ul id='zDRlY'></ul>