用java制作一个递归的文本打印方法

用java制作一个递归的文本打印方法,java,string,stringbuilder,Java,String,Stringbuilder,我必须做一个这样的程序。首先从输入中获取一个数字,然后获取(number)*字符串 例如: 2 a b 或 然后在输出中打印如下内容: Math.max(a, b) 或 我想用这段代码生成Math.max方法语法。我希望你能理解 另一个输入和输出示例: 输入= 4 a b c d 输出= Math.max(a, Math.max(b, Math.max(c, d))) 有人能帮我吗 我为它写的代码,你能给我一些修改建议吗 import java.util.*; public class

我必须做一个这样的程序。首先从输入中获取一个数字,然后获取(number)*字符串

例如:

2
a b

然后在输出中打印如下内容:

Math.max(a, b)

我想用这段代码生成Math.max方法语法。我希望你能理解

另一个输入和输出示例:

输入=

4
a b c d
输出=

Math.max(a, Math.max(b, Math.max(c, d)))
有人能帮我吗

我为它写的代码,你能给我一些修改建议吗

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int n = input.nextInt();
    String[] r = new String[n];
    for (int i = 0; i < n; i++) {
      r[i] = input.next();
    }
    printmax(r);

  }
  public static int i = 0 , j = 0;
  public static boolean last = false;
  public static void printmax(String [] r){
    if (last == true) {
      System.out.print(r[r.length - 1]);
      while (j < r.length - 1){ System.out.print(")");
        j++;
      }
    }
    if (r.length == 2) System.out.print("Math.max(" +r[0] + ", " + r[1] + ")");
    if (r.length > 2) {
      while (i < r.length -1) {
        if (i == r.length -2) last = true;
        System.out.print("Math.max(" + r[i] + ", ");
        i++;
        printmax(r);
      }
    }
  }
}
import java.util.*;
公共班机{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int n=input.nextInt();
字符串[]r=新字符串[n];
对于(int i=0;i2){
而(i
您可以使用以下代码来实现上述功能,这里我递归调用maxElement()函数来实现类似Math.max(a,Math.max(b,Math.max(c,d))这样的功能

publicstaticvoidmain(字符串参数[]){
int length=2;//这里从扫描仪读取输入
字符串[]数组={“a”,“b”};//这里从扫描仪读取此输入
字符串max=maxElement(数组,0,长度);
系统输出打印项次(最大值);
}
公共静态字符串maxElement(字符串[]开始,整数索引,整数长度){

如果(index您可以使用以下代码来实现上述功能,这里我递归调用maxElement()函数来实现类似Math.max(a,Math.max(b,Math.max(c,d))的功能)

publicstaticvoidmain(字符串参数[]){
int length=2;//这里从扫描仪读取输入
字符串[]数组={“a”,“b”};//这里从扫描仪读取此输入
字符串max=maxElement(数组,0,长度);
系统输出打印项次(最大值);
}
公共静态字符串maxElement(字符串[]开始,整数索引,整数长度){

如果(index你需要这样做

首先定义一个函数
maxElement
,它将变量数组作为参数

public static maxElement(String[] variables) {
    return maxElementBis(variables,0);
}
然后调用第二个函数:
maxElementBis
,它接受一个额外的参数,该参数表示我们正在处理的变量的索引

public static String maxElementBis(String[] variables, int index) {
    if (variables.length < 2) 
        return "not enought variables";
    if (variables.length - index == 2)
        return "Math.max("+ variables[index]+","+variables[index + 1]+")";
    return "Math.max("+ variables[index]+","+maxElementBis(variables,index + 1)+")";

}
公共静态字符串maxElementBis(字符串[]变量,int索引){
如果(变量长度<2)
返回“没有足够的变量”;
if(variables.length-index==2)
返回“Math.max”(“+variables[index]+”,“+variables[index+1]+”);
返回“Math.max(“+variables[index]+”,“+maxElementBis(variables,index+1)+”);
}
如果数组包含少于两个变量,则无法执行所需操作

如果只剩下两个变量,这就是停止条件,您可以直接返回
Math.max(v1,v2)


否则递归调用函数
maxementbis

您需要这样做

首先定义一个函数
maxElement
,它将变量数组作为参数

public static maxElement(String[] variables) {
    return maxElementBis(variables,0);
}
然后调用第二个函数:
maxElementBis
,它接受一个额外的参数,该参数表示我们正在处理的变量的索引

public static String maxElementBis(String[] variables, int index) {
    if (variables.length < 2) 
        return "not enought variables";
    if (variables.length - index == 2)
        return "Math.max("+ variables[index]+","+variables[index + 1]+")";
    return "Math.max("+ variables[index]+","+maxElementBis(variables,index + 1)+")";

}
公共静态字符串maxElementBis(字符串[]变量,int索引){
如果(变量长度<2)
返回“没有足够的变量”;
if(variables.length-index==2)
返回“Math.max”(“+variables[index]+”,“+variables[index+1]+”);
返回“Math.max(“+variables[index]+”,“+maxElementBis(variables,index+1)+”);
}
如果数组包含少于两个变量,则无法执行所需操作

如果只剩下两个变量,这就是停止条件,您可以直接返回
Math.max(v1,v2)


否则你递归调用你的函数
maxElementBis

到目前为止你尝试了什么?你不能期望有人给你完整的答案。@TrishulSinghChoudhary我不是在寻找完整的答案!只需要知道如何实现它将你的输入放入数组中。现在取数组的最后两个元素y并使用StringBuffer形成字符串。通过在数组中向后移动,以递归方式继续执行此操作。在循环中,添加
“Math.max(“
,名称和
”,“
”;除非留下一个名称,然后再添加正确的
”)
-建议使用
StringBuilder
保存结果text@user85421您能给我一个答复吗?我们将不胜感激:)到目前为止,您尝试了什么?您不能期望有人给您完整的答案。@TrishulSinghChoudhary我不是在寻找完整的答案!只需要一个如何实现它的想法将您的输入放入数组中。现在使用StringBuffer获取数组的最后2个元素并形成一个字符串。通过在在一个循环中,添加
“Math.max(“
,名称和
”,“
”;除非留下一个名称,然后再添加正确的
”),
-建议使用
StringBuilder来保存结果text@user85421您能给我一个答复吗?我们将不胜感激:)
public static String maxElementBis(String[] variables, int index) {
    if (variables.length < 2) 
        return "not enought variables";
    if (variables.length - index == 2)
        return "Math.max("+ variables[index]+","+variables[index + 1]+")";
    return "Math.max("+ variables[index]+","+maxElementBis(variables,index + 1)+")";

}