繁体   English   中英

递归方法返回并打印String的Int数

[英]Recursive method return and print Int number of String

我在使用递归方法时遇到了麻烦。 这将返回并在我的主要方法中输出W的X数。X是命令行上的正整数(arg [0]),W是命令行上的String(arg [1])。 因此,对于任何数字,它将多次打印字符串。

例如,我的第一个命令行参数是“ 4”,而我的第二个命令行参数是Hello。

输出应打印为字符串:

“你好你好你好你好”

我的参数是整数和我相信的字符串时遇到问题:(?

我的代码atm:

public static void main(String[] args){
      int number = new Integer(0);
      String word = new String("");
      number = Integer.parseInt(args[0]);
      word = args[1];

      String method = recursive1.method1(word);
      System.out.println(method);
   }

   public static String method1(String word, int number) {
      if (number < 0){
         return 0;
      }
      else{
         return word + method1(number-1);
      }
   }

}

尝试

public static String method1(String word, int number) {
    if (number < 1){
       return ""; // seems that if number is 0 or less, nothing will be printed
    }
    return word + method1(word, number-1);  
}

要打印它:

System.out.println(method1(word, number));

您的代码有几个问题。 我在必要时添加了评论;

public static void main(String[] args) {
    ... // skipped previous lines
    // No need to use class name as main is static and method1 is also static.
    String method = method1(word, number); // Call the method with 2 parameters
    System.out.println(method);
}

// With an else - improves readability
public static String method1(String word, int number) {
    if (number == 0) { // If it is zero, return a blank string
        return ""; // return a blank string and not 0(int)
    } else {
        return word + method1(word, number - 1); // method1 requires 2 parameters
    }
}

// Without an else - unnecessary else removed
public static String method1(String word, int number) {
    if (number == 0) { // If it is zero, return a blank string
        return ""; // return a blank string and not 0(int)
    }
    // Removed the else as its really not necessary
    return word + method1(word, number - 1); // method1 requires 2 parameters
}

附带说明一下,在main()方法中,您实际上有2行不必要的代码行。

  // int number = new Integer(0); // not needed
  // String word = new String(""); // not needed
  int number = Integer.parseInt(args[0]); // Since you're over-writing the value anyways
  String word = args[1]; // Since you're over-writing the value anyways

你写:

int number = new Integer(0);

最好是:

int number = 0;

还是为什么不

int number = Integer.parseInt(argv[0]);

马上? 最初的0有什么用?

然后,当然,当您定义带有n个参数的方法时,请始终使用n个参数来调用它。

String result = method1(word, number);

试试这个代码。 需要进行一些更改。

public static void main(String[] args){
  int number = new Integer(5); // you can comment this line when providing input from command line
  String word = new String("hello");// you can comment this line when providing input from command line
  number = Integer.parseInt(args[0]);
  word = args[1];

  String method = method1(word,number);
  System.out.println(method);
}

public static String method1(String word, int number) {
  if (number == 0){
     return "";
  }
  else{
     return word + method1(word,number-1);
  }
}

暂无
暂无

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

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