递归方法返回并打印字符串的Int数

时间:2014-02-06 08:52:10

标签: java recursion methods command-line return

我的递归方法遇到了问题。这将返回并在我的main方法中打印X的X数.X是命令行上的正整数(arg [0]),W是命令行上的字符串(arg [1])。 因此,对于任何数字,它将多次打印字符串。

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

输出应打印为字符串:

  

“HelloHelloHelloHello”

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

我的代码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);
      }
   }

}

4 个答案:

答案 0 :(得分:3)

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));

答案 1 :(得分:3)

您的代码存在一些问题。我已经在必要时添加了评论;

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

答案 2 :(得分:1)

你写道:

int number = new Integer(0);

最好是:

int number = 0;

或者为什么不

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

马上?用于初始0的目的是什么?

然后,当然,当你用n个参数定义一个方法时,总是用n个参数调用它。

String result = method1(word, number);

答案 3 :(得分:0)

试试这段代码。有一些变化需要做。

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);
  }
}