【问题标题】:How can i make a method to change the middle character of user input without already know the the word?如何在不知道单词的情况下更改用户输入的中间字符?
【发布时间】:2016-12-04 16:26:15
【问题描述】:

我对 java 很陌生,我需要一种方法,用另一个字母替换任何用户输入的中间字母。无论用户输入“abcdef”、“frogs”还是“pizza”,如果我愿意,它会将中间字母更改为“Q”。我对此很陌生,所以我也不知道如何使用数组。我想过使用 replaceCharAt()str.length() 但我不知道如何将它们与未知输入一起使用。任何帮助将不胜感激。

编辑:我已经包含了我的课程和我的驱动程序课程以显示我正在处理的内容。我需要将用户输入 dnaCode 的中间字母更改为“Q”。

到目前为止我的班级:

public class ComputerMicrobe
{

  public String name;
  public String dnaCode;

//***********************************************************************
  //perameters

  public ComputerMicrobe(String newDnaCode, String newName)
  {
   this.name(newName);
   this.dnaCode(newDnaCode);
  }// end perameters



//*************************************************************
 // default
 public ComputerMicrobe()
 {
  this("12345", "ABCDEF");

 }// end default

//****************************************************************

 // accessors
 public String getName()
 {
  return this.name;
 }// end name accesseor

 public String getDnaCode()
 {
  return this.DnaCode;
 }// end dnaCode accessor

//***********************************************************************

 public void setName(String newName)
 {
  this.name = newName;
 }// end name mutator

 public void setDnaCode(String newDnaCode)
 {
  this.dnaCode = newDnaCode;
 }// end dnaCode mutator

//************************************************************************

我的司机班(由于作业无法编辑):

public static void main (String[] args)
{
  Scanner stdIn = new Scanner(System.in);
  String name;    //Auxiliar ComputerMicrobe name
  String dNACode;   //Auxiliar ComputerMicrobe DNA Code
  ComputerMicrobe a, b, c; // ComputerMicrobe objects

  System.out.println("Enter name of first ComputerMicrobe");
  name = stdIn.next();
  System.out.println("Enter DNA Code for first ComputerMicrobe");
  dNACode = stdIn.next();
  a = new ComputerMicrobe(name, dNACode);

  System.out.println("Enter name of second ComputerMicrobe");
  name = stdIn.next();
  System.out.println("Enter DNA Code for second ComputerMicrobe");
  dNACode = stdIn.next();
  b = new ComputerMicrobe(name, dNACode);

  System.out.println("Initial set of ComputerMicrobes");
  System.out.println(a);
  System.out.println(b);

  System.out.println("ComputerMicrobe a after mutation");
  a.mutate();
  System.out.println(a);

  System.out.println("ComputerMicrobe b after swap");
  b.swap();
  System.out.println(b);

  System.out.println("ComputerMicrobe c after reproduction of a and b");
  c = a.reproduce(b);
  System.out.println(c);

  System.out.println("ComputerMicrobe b after mutation and swap");
   b.mutate().swap();
  System.out.println(b);

  System.out.println("ComputerMicrobe b after invasion of swap a");
  b.invadedBy(a.swap());
  System.out.println(b);
} // end main

【问题讨论】:

  • 更多代码...告诉我们你是如何做到这一点的!
  • 没有理由传递长度,因为您将拥有它与输入。当你有偶数个字符时会发生什么?
  • 如果有帮助,我已经更新了我的问题
  • ΦXocę 웃 Пepeúpa ツ 我附上了我的代码

标签: java string input replace character


【解决方案1】:

首先你应该检查 userInput 参数是否有效,我的意思是它的长度应该是奇数并且大于 2。

if(userInput.length() %2 ==0 || userInput.length()<3)
         throw new IllegalArgumentException("userInput length should be odd and greater than 2");

然后你就可以做你想做的事了

int middleIndex = userInput.length()/2;
return userInput.substring(0,middleIndex) + 'Q' + userInput.substring(middleIndex+1);

整个方法应该是这样的:

    public String replaceCharaterAT(String userInput) {
         if (userInput.length() % 2 == 0 || userInput.length() < 3)
             throw new IllegalArgumentException("userInput length should be odd and greater than 2");
         int middleIndex = userInput.length() / 2;
         return userInput.substring(0, middleIndex) + 'Q' + userInput.substring(middleIndex + 1);
}

【讨论】:

    【解决方案2】:

    你可以试试这个;)

    BufferedReader z=new BufferedReader(new InputStreamReader(System.in));
            String ch=(z.readLine());
            char valeur = 'Q';  //chat for replace
            String res="";
            for(int i=0;i<ch.length()/2;i++){
                res+=ch.charAt(i);
            }
            res+=valeur;
            for(int i=1+ch.length()/2;i<ch.length();i++){
                res+=ch.charAt(i);
            }   
            System.out.println(res);
    

    【讨论】:

      【解决方案3】:

      我觉得是这样的。。

      public static String replaceChar(String word, String key)
      {
      
          int length = word.length();
      
      
          int center = length/2;
      
          String before = word.substring(0, center - 1);
      
          String after = word.substring(center + 1, length);
      
      
          return before + key + after;
      }
      
      
      public static void main(String[] args) {
          System.out.println(replaceChar("abcdef", "q"));
      }
      

      【讨论】:

      • abcdef的中间字符是什么?
      • 并非如此。偶数单词中没有中间字符。您的算法替换了两个“中间”字符,但是当您将“foo”作为单词时它会做什么?
      猜你喜欢
      • 2018-03-24
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2021-05-02
      • 2015-10-30
      相关资源
      最近更新 更多