亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

來自控制臺的 Java 遞歸

來自控制臺的 Java 遞歸

智慧大石 2021-12-10 15:12:00
//當我運行程序時,什么都不打印。它就像一個無限循環 // 在輸入我的字符串控制臺時。// 編寫一個 Java 程序,它接受來自控制臺的字符串輸入 // 并使用遞歸反轉它。//反轉字符串后打印結果。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;    }}
查看完整描述

2 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

您不需要在功能中傳遞掃描儀。從用戶那里獲取輸入并將其傳遞給遞歸函數。首先傳遞字符串,起始索引和最后一個索引。這是代碼:


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

  }


查看完整回答
反對 回復 2021-12-10
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

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

}

}


查看完整回答
反對 回復 2021-12-10
  • 2 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號