【问题标题】:Choose a random letter and change it (JAVA)选择一个随机字母并更改它(JAVA)
【发布时间】:2015-02-27 11:39:37
【问题描述】:

大家好,我是 Java 新手,我的代码需要帮助

/**
 * @(#)Changing.java
 *
 *
 * @author 
 * @version 1.00 2015/2/27
 */

import java.util.Random;

public class Changing {

    public static void main(String[] args) {
        Random generator = new Random();
        int num1;   
        String phrase = "This is a pencil";

        int len = phrase.length();
        num1 = generator.nextInt(len);
        char c = phrase.charAt(num1, 'x');
        String mut1 = phrase.replace(c, 'x');
        System.out.println(mut1);
    }
}

我需要制作程序来随机选择一个字母并将其更改为 X

谢谢

【问题讨论】:

  • 问题出在哪里?
  • @Jens - 这个:char c = phrase.charAt(num1, 'x');:P
  • 正如@TheLostMind 指出的那样,您是否 100% 确定 .charAt 方法对字符串的作用?
  • 应该是char c = phrase.charAt(num1);。但即便如此,难道不会有1:4左右得到mut1 = "This isXa pencil的机会吗?
  • @WonderWorld 索引处的字符被选中。然后该字符被替换,其中包括所有出现。 replace 方法声明:Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar..

标签: java string random replace char


【解决方案1】:

中删除“x”是正确的
char c = phrase.charAt(num1, 'x');

您可能还需要考虑使用 if 语句来确保您不会以 “Thisxisxaxpencil”作为答案。当然,除非您对该输出感到满意。见下面的代码......

import java.util.Random;
public class Changing {

public static void main(String[] args) {
    Random generator = new Random();
    int num1;   
    String phrase = "This is a pencil";

    int len = phrase.length();
    char c = ' ';
    while(c == ' '){
        num1 = generator.nextInt(len);
        c = phrase.charAt(num1);
    }
    String mut1 = phrase.replace(c, 'x');
    System.out.println(mut1);

}

编辑 您可能还需要考虑创建一个 char 数组而不是使用 .replace()

这将使您不会替换字符串中的每个“s”或每个“i”。 请参阅下面的代码...

    public static void main(String[] args) {
    Random generator = new Random();
    int num1 = 0;   
    String phrase = "This is a pencil";

    int len = phrase.length();
    char c = ' ';
    while(c == ' '){//to ensure we don't x out a space
        num1 = generator.nextInt(len);
        c = phrase.charAt(num1);
    }
    char[] phraseArray = phrase.toCharArray();//make string into a char[]
    phraseArray[num1] = 'x';//replace char the random char index with 'x' This replaces the random letter for you.
    String mut1 = new String(phraseArray);//change your char[]  back into a string
    System.out.println(mut1);//print results
}

【讨论】:

  • 这仍然会用 X 替换所有 i。这不应该发生。
  • 如果您不想使用 charArray,请使用 String sub1 = phrase.substring(0, num1) + 'X' + phrase.substring(num1+1);
【解决方案2】:

char c = phrase.charAt(num1, 'x'); 更改为char c = phrase.charAt(num1);

没有接受两个参数的 charAt 方法。

【讨论】:

    【解决方案3】:

    环顾四周并处理了几个问题后,我发现StringBuilder() 完全符合您的要求。

        Random generator = new Random();
        int num1 = 0;
        StringBuilder phrase = new StringBuilder("This is a pencil");
        int len = phrase.length();
        char c = ' ';
        while (c == ' ') {
            num1 = generator.nextInt(len);
            c = phrase.charAt(num1);
        }
        phrase.setCharAt(num1, 'X');   // <-- i think this is a nice way to do it.
        //String sub1 = phrase.substring(0, num1) + 'X' + phrase.substring(num1+1); // Not so elegant as StringBuilder().
        System.out.println(phrase);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      相关资源
      最近更新 更多