【问题标题】:how to use recursion for converting String to int如何使用递归将 String 转换为 int
【发布时间】:2014-02-21 15:38:47
【问题描述】:

我想使用递归将 String 输入转换为 int。这是我想出的代码,但如果我输入的是 123456,它只会返回 124。如果我输入 1234567,则会出错。

import java.util.*;
public class Problem1 {
static int x =0;
static int counter = 0;
//input
 public static void main(String[] args) {
 Scanner scan = new Scanner (System.in);
 String s= scan.nextLine();
 System.out.println(recursive(s));

}
 //recursive method
public static int recursive(String s){
    if(s.length()==1){
        x=(x*10)+ Integer.parseInt(s.substring(0,1));
        return x;
    }
    else{
        x = (x*10)+Integer.parseInt(s.substring(0,1));
        counter++;
        return recursive(s.substring(counter,s.length()-1));


    }

    }
}

【问题讨论】:

  • 您的 o/p 期望什么?为什么要乘以 10?

标签: java string recursion int


【解决方案1】:

试试这样的:

从你的字符中减去 '0' 字符的 ASCII 码返回一个整数:

public class StringRecursion {
    static int counter = 0;

    public static void main(String[] args) {
        System.out.println(convertStringToInt("123456"));
    }

    public static int convertStringToInt(String input) {
        if (input.length() == 1)
            return input.charAt(0) - '0';

        int value = convertStringToInt(input.substring(0, input.length() - 1));
        counter++;
        return value * 10 + input.charAt(counter) - '0';

    }
}

【讨论】:

    【解决方案2】:

    我知道这是一种迟到的反应,但你可以尝试这样的事情:-

        private static int stringToInt(String string) {
        if (string.length() == 0) {
            return 0;
        }
        int rv;
        int num = string.charAt(string.length() - 1) - '0';
        String restOfTheString = string.substring(0, string.length() - 1);
        rv = stringToInt(restOfTheString) * 10 + num;
        return rv;
    }
    

    【讨论】:

      【解决方案3】:

      试试这样:

      public static int conStrToInt(String str) {
      
      
          if(str.length()==0)
          {
              return 0;
          }
          char cc = str.charAt(0);
          String ros = str.substring(1);
      
          int factor=1;
          for(int i=0;i<str.length()-1;i++)
              factor*=10;
      
          factor=factor*(cc-'0');
          return factor+conStrToInt(ros);
      
      }
      

      【讨论】:

        【解决方案4】:
        public static int computeStr(String str) {
                if (str.equals("")) {
                    return 0;
                }
        
                int x = 1;
                for (int i = 0; i < str.length() - 1; i++) {
                    x = x * 10;
                }
                x = x * Integer.parseInt(str.substring(0, 1));
                return x + computeStr(str.substring(1));
            }
        

        例如:“2432”为 (2 * 1000) + (4 * 100) + (3*10) + (2*1) = 2432 该算法从 2432 的第一个位置 (2) 开始

        【讨论】:

          【解决方案5】:
          recursion("1234567", 0, 1)
          

          以上代码将使用递归将字符串“1234567”转换为int。您必须传递要转换的字符串,然后是 0 和 1。

          public static int recursion(String s, int result, int place) {
              result += place * Integer.parseInt(s.charAt(s.length() - 1) + "");
              if(s.length() == 1) {
                  return result;
              }
              else {
                  return recursion(s.substring(0, s.length() - 1), result, place * 10);
              }
          }
          

          【讨论】:

            【解决方案6】:
            import java.util.Scanner;
            
            public class Problem1 {
                static int x = 0;
                static int counter = 0;
            
                // input
                public static void main(String[] args) {
                    Scanner scan = new Scanner(System.in);
                    String s = scan.nextLine();
                    System.out.println(recursive(s));
            
                }
            
                // recursive method
                public static int recursive(String s) {
                    if (s.length() == 1) {
                        x = (x * 10) + Integer.parseInt(s.substring(0, 1));
                        return x;
                    } else {
                        x = (x * 10) + Integer.parseInt(s.substring(0, 1));
                        counter++;
                        return recursive(s.substring(1, s.length()));
            
                    }
            
                }
            }
            

            【讨论】:

              【解决方案7】:

              开始的几点说明:

              1. 如果您正在执行递归,您可能不想使用成员变量。这样做没有错,但不是典型的模式(您的 x 变量)。
              2. 通过递归传递状态通常很方便,尽管您不必这样做(即 x 的当前值)。
              3. 您的情况有点奇怪,因为您必须更改每个子解析的当前解析值(每次移动 10);让它变得更复杂一些。
              4. 如果您要将 x 保留为成员变量(在这种情况下似乎确实有意义),则无需从递归返回任何内容。
              5. 真的不能只使用 Integer.parseInt() 吗?

              代码可以更简单,例如:

               void recursive (String s)
               {
                 if (s.length() == 0) return 0;
                 x = x * 10 + Integer.parseInt(s.substring(0, 1));
                 recursive(s.substring(1));
               }
              

              【讨论】:

                【解决方案8】:

                查看您的静态 counter 变量。你每次都在增加它。但是你只想让子字符串从 1 开始(所以切断第一个“字母”)。

                所以不要使用:

                counter++;
                return recursive(s.substring(counter,s.length()-1));
                

                考虑使用:

                return recursive(s.substring(1));  // you even don't really need the length
                

                因为String s参数如下:

                1st call: 1234567
                2nd call: 234567
                3rd call: 34567
                4th call: 4567
                ...
                

                所以,你只需要剪掉第一个字母。

                顺便说一句:您的示例“项目”非常有趣;)

                【讨论】:

                  猜你喜欢
                  • 2010-11-04
                  • 2018-08-28
                  • 2012-11-22
                  • 2011-05-29
                  • 2011-11-19
                  • 2011-12-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-07-29
                  相关资源
                  最近更新 更多