Java 将一个字母的一个实例替换为另外两个递归字母的代码

Java 将一个字母的一个实例替换为另外两个递归字母的代码,java,string,recursion,input,methods,Java,String,Recursion,Input,Methods,我试图用3个b替换a的每个实例,我有代码用一个b替换它,但我不知道如何将字母的多个实例放进去。我会使用字符串而不是字符,因为它不能容纳更多的一个字符,但这给了我一个错误 import java.util.Scanner; public class replace { public static void main(String[] args) { Scanner input = new Scanner(System.in); String words = input.

我试图用3个b替换a的每个实例,我有代码用一个b替换它,但我不知道如何将字母的多个实例放进去。我会使用字符串而不是字符,因为它不能容纳更多的一个字符,但这给了我一个错误

import java.util.Scanner;
public class replace 
{
  public static void main(String[] args) 
  {
    Scanner input = new Scanner(System.in);
    String words = input.nextLine();
    char from = 'a';
    char to = 'b';

    System.out.println(replace(words, from, to));
  }

  public static String replace(String s, char from, char to){
    if (s.length() < 1) 
    {
      return s;
    }
    else 
    {
      char first = from == s.charAt(0) ? to : s.charAt(0);
      return first + replace(s.substring(1), from, to);
    }
  }
}
import java.util.Scanner;
公共类替换
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
String words=input.nextLine();
char from='a';
char to='b';
System.out.println(替换(字,从,到));
}
公共静态字符串替换(字符串s、字符from、字符to){
如果(s.长度()<1)
{
返回s;
}
其他的
{
char first=from==s.charAt(0)到:s.charAt(0);
返回第一个+替换(s.子串(1)、从、到);
}
}
}
导入java.util.Scanner;
公共类替换
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
String words=input.nextLine();
字符串from=“a”;
字符串to=“bbb”;
System.out.println(替换(字,从,到));
}
公共静态字符串替换(字符串s、字符串from、字符串to){
如果(s.长度()<1)
{
返回s;
}
其他的
{
字符串first=from.equals(s.substring(0,1))?to:s.substring(0,1);
返回第一个+替换(s.子串(1)、从、到);
}
}
}

单词=单词。替换(“a”、“bbb”)难道你不能使用这个程序吗?不过,在处理字符串时使用运算符?
import java.util.Scanner;
public class replace 
{
  public static void main(String[] args) 
  {
    Scanner input = new Scanner(System.in);
    String words = input.nextLine();
    String from = "a";
    String to = "bbb";

    System.out.println(replace(words, from, to));
  }

  public static String replace(String s, String from, String to){
    if (s.length() < 1) 
    {
      return s;
    }
    else 
    {
      String first = from.equals(s.substring(0,1)) ? to : s.substring(0,1);
      return first + replace(s.substring(1), from, to);
    }
  }
}