Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使单词中间的字母随机化,同时保持第一个字母和最后一个字母不变。 import java.util.Scanner; /** * *@author Cutuk */ 公共类JavaApplication3{ /** *@param指定命令行参数 */ 公共静态void main(字符串[]args){ 字符串a; 扫描仪输入=新扫描仪(系统输入); a=in.nextLine(); char first=a.charAt(0); 系统输出打印(第一); int v=a.长度()-1; char last=a.charAt(v); int k=a.长度(); int随机=0; 字符x='\u0000'; 字符中间='\u0000'; 对于(int i=1;i_Java - Fatal编程技术网

使单词中间的字母随机化,同时保持第一个字母和最后一个字母不变。 import java.util.Scanner; /** * *@author Cutuk */ 公共类JavaApplication3{ /** *@param指定命令行参数 */ 公共静态void main(字符串[]args){ 字符串a; 扫描仪输入=新扫描仪(系统输入); a=in.nextLine(); char first=a.charAt(0); 系统输出打印(第一); int v=a.长度()-1; char last=a.charAt(v); int k=a.长度(); int随机=0; 字符x='\u0000'; 字符中间='\u0000'; 对于(int i=1;i

使单词中间的字母随机化,同时保持第一个字母和最后一个字母不变。 import java.util.Scanner; /** * *@author Cutuk */ 公共类JavaApplication3{ /** *@param指定命令行参数 */ 公共静态void main(字符串[]args){ 字符串a; 扫描仪输入=新扫描仪(系统输入); a=in.nextLine(); char first=a.charAt(0); 系统输出打印(第一); int v=a.长度()-1; char last=a.charAt(v); int k=a.长度(); int随机=0; 字符x='\u0000'; 字符中间='\u0000'; 对于(int i=1;i,java,Java,对于您的随机,请尝试以下方法:随机生成器=新随机(System.currentTimeMillis());您的方法是错误的:当您随机选择中间字母时,无法保证打印出单词中间的所有字母(因此,其他字母不会重复) 有几种方法可以解决此问题: 每次生成随机索引时,在booleans数组中标记该索引。数组的长度等于单词的长度。在使用生成的每个新索引之前,请检查数组;如果标记了索引,则继续生成新的随机索引,直到找到空索引为止 创建单词内部字母的整数索引数组(即从1到长度-1,包括1)。对数组执行一次排序,然

对于您的随机,请尝试以下方法:随机生成器=新随机(System.currentTimeMillis());

您的方法是错误的:当您随机选择中间字母时,无法保证打印出单词中间的所有字母(因此,其他字母不会重复)

有几种方法可以解决此问题:

  • 每次生成随机索引时,在
    boolean
    s数组中标记该索引。数组的长度等于单词的长度。在使用生成的每个新索引之前,请检查数组;如果标记了索引,则继续生成新的随机索引,直到找到空索引为止
  • 创建单词内部字母的整数索引数组(即从1到长度-1,包括1)。对数组执行一次排序,然后使用乱序索引拾取中间字母
  • 与2类似,只是将所有中间字母放在一个数组中,然后将其洗牌

  • 如果我理解你的问题,我建议你从构建一个开始,然后使用并最终用一个like构建你的return
    String

    private静态字符串shuffleLetters(字符串输入){
    if(in==null | | in.length()<3){
    返回;
    }
    char first=in.charAt(0);
    char last=in.charAt(in.length()-1);
    列表字符=新的ArrayList();
    for(char ch:in.substring(1,in.length()-1.toCharArray()){
    添加字符(ch);
    }
    收藏。洗牌(字符);
    StringBuilder sb=新的StringBuilder();
    某人(先);
    for(char ch:chars){
    某人附加(ch);
    }
    某人追加(最后);
    使某人返回字符串();
    }
    
    您只需创建一个
    列表
    即可存储生成的随机数

    这是您上面的代码,经过清理,具有有意义的命名和用于查找历史记录的
    列表:

    private static String shuffleLetters(String in) {
        if (in == null || in.length() < 3) {
            return in;
        }
        char first = in.charAt(0);
        char last = in.charAt(in.length() - 1);
        List<Character> chars = new ArrayList<>();
        for (char ch : in.substring(1, in.length() - 1).toCharArray()) {
            chars.add(ch);
        }
        Collections.shuffle(chars);
        StringBuilder sb = new StringBuilder();
        sb.append(first);
        for (char ch : chars) {
            sb.append(ch);
        }
        sb.append(last);
        return sb.toString();
    }
    
    “防止随机数被使用两次”机制假定“洗牌”有时允许中间字符与自身交换,您可以执行以下操作:

    do {
        random = rnd.nextInt(input.length() - 2) + 1;
    } while (rndHistory.contains(random));       // check the history
    
    rndHistory.add(random);                      // add to the history
    
    它处理单词为null、一个字符、两个字符或两个或多个字符的情况,并在一次运行中生成以下输出:

    private static final String[] TEST_WORDS = {"apple", "banana", "pear", "raspberry", "cucumber", "watermelon", "a", "ab", "", null};
    
    public static void main(String[] args) 
    {
        for (String word: TEST_WORDS)
        {
            System.out.println(shuffleInside(word));
        }
    }
    
    private static String shuffleInside(String word)
    {
        String ret = word;
    
        if (word != null)
        {
            Random r = new Random();
    
            int strLen = word.length();
    
            if (strLen > 2)
            {
                char[] middleChars = word.substring(1, strLen - 1).toCharArray();
                shuffle(middleChars, r);
                ret = word.charAt(0) + new String(middleChars) + word.charAt(strLen - 1);
            }
        }
    
        return ret;
    }
    
    private static void shuffle(char[] chars, Random r)
    {
        for (int i = chars.length - 1; i > 0; i--)
        {
          int index = r.nextInt(i + 1);
          char c = chars[index];
          chars[index] = chars[i];
          chars[i] = c;
        }
    }
    

    “避免重复”:这意味着什么?请为变量使用有意义的名称,否则我们很难分析代码。
    do {
        random = rnd.nextInt(input.length() - 2) + 1;
    } while (rndHistory.contains(random));       // check the history
    
    rndHistory.add(random);                      // add to the history
    
    private static final String[] TEST_WORDS = {"apple", "banana", "pear", "raspberry", "cucumber", "watermelon", "a", "ab", "", null};
    
    public static void main(String[] args) 
    {
        for (String word: TEST_WORDS)
        {
            System.out.println(shuffleInside(word));
        }
    }
    
    private static String shuffleInside(String word)
    {
        String ret = word;
    
        if (word != null)
        {
            Random r = new Random();
    
            int strLen = word.length();
    
            if (strLen > 2)
            {
                char[] middleChars = word.substring(1, strLen - 1).toCharArray();
                shuffle(middleChars, r);
                ret = word.charAt(0) + new String(middleChars) + word.charAt(strLen - 1);
            }
        }
    
        return ret;
    }
    
    private static void shuffle(char[] chars, Random r)
    {
        for (int i = chars.length - 1; i > 0; i--)
        {
          int index = r.nextInt(i + 1);
          char c = chars[index];
          chars[index] = chars[i];
          chars[i] = c;
        }
    }
    
    apple
    bnaana
    paer
    rerrsbpay
    cbuemucr
    waomteerln
    a
    ab
    
    null