【问题标题】:Java Recursion from console来自控制台的 Java 递归
【发布时间】:2018-10-27 13:45:35
【问题描述】:

//当我运行程序时,什么也没有打印出来。它就像一个无限循环 //在输入我的字符串控制台时。 //编写一个Java程序,接受来自控制台的字符串输入 //并使用递归反转它。在字符串 //reversed 后打印结果。

import java.util.Scanner;

public class ReverseTry {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a sentence and I will reverse it");
        reverseLine(input);
    }

    public static Scanner reverseLine(Scanner input) {
        if (!input.hasNextLine()) {
            return input;
        } else {
            //String word = input.nextLine();
            //return reverseLine(input) + " " + word;

            String line = input.nextLine();
            reverseLine(input);
            System.out.println(line);
        }
        return input;
    }
}

【问题讨论】:

标签: java recursion


【解决方案1】:

您不需要在函数中传递扫描仪。从用户那里获取输入并将其传递给递归函数。首先传递字符串、起始索引和最后一个索引。 这是代码:

 public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a sentence and I will reverse it : ");
    String line = input.nextLine();
    String reverse = reverseLine(line, 0, line.length() - 1);
    System.out.println(reverse);
  }

Reverseline 函数接受一个输入并在指定位置交换输入字符并递归调用。

  public static String reverseLine(String input, int startIndex, int endIndex) {
    if (startIndex >= endIndex) {
      return input;
    } else {
      input = swap(input, startIndex, endIndex);
      return reverseLine(input, startIndex + 1, endIndex - 1);
    }
  }

这个函数只交换字符串的字符。

  public static String swap(String input, int start, int end) {
    char[] arr = input.toCharArray();
    arr[end] = input.charAt(start);
    arr[start] = input.charAt(end);
    return String.valueOf(arr);
  }

【讨论】:

    【解决方案2】:
    import java.util.Scanner;
    
    public class ReverseTry {
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter a sentence and I will reverse it");
            reverseLine(input); //no need to pass Scanner object as input to method since the return value isn't used anyway
        }
    
        public static Scanner reverseLine(Scanner input) {
            if (!input.hasNextLine()) {
                return input;
            } else {
                //String word = input.nextLine();
                //return reverseLine(input) + " " + word;
    
                String line = input.nextLine(); 
                reverseLine(input); // after you get the line over here, 
                                    //this function will be called again. 
                                    //In the recursively called function, 
                                    //it will again ask you for input and 
                                    //once you give it, again call itself 
                                    //and this will go on forever. Instead 
                                    //of this endless recursive call, you 
                                    //need to give a base condition on which 
                                    //this method should return a useful value 
                                    //without calling itself. For example, you 
                                    //know that there is nothing to be 
                                    //reversed in a single character string ...
                System.out.println(line);
            }
            return input;
        }
    }
    

    您可能想要以下内容,

    import java.util.*;
    
    public class ReverseTry {
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
    
        System.out.println("Enter a sentence and I will reverse it");
        String s = input.nextLine(); //no need to pass Scanner object into the reverseline method. Scanner object is used to read the console input only once
        s = reverseLine(s); // here is where the reversing logic goes
        System.out.println(s); // printing out the reversed string
    }
    
    public static String reverseLine(String s) {
    
        if(s.length() == 1 ) { //base case --> there is nothing to reverse when the string length is 1
            return s;
        }
        return s.substring(s.length()-1)+reverseLine(s.substring(0,s.length()-1)); //recursion logic
    }
    }
    

    【讨论】:

    • 没有解释原件有什么问题。仅仅为他们编写代码(当它可能是家庭作业时)是相当糟糕的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2016-09-22
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多