【问题标题】:How can I change the chars of a String in a method? (same chars, randomly order)如何在方法中更改字符串的字符? (相同的字符,随机排序)
【发布时间】:2016-06-15 02:50:17
【问题描述】:

//请帮我修复这个代码。我想返回一个与发送字符串具有相同字符但顺序不同的字符串。

public static String mix(String s){
        int random;
        int n= s.length();
        int [] control = new int[n];
        String miX="";

    for(int i=0 ; i < n ; i++){
        random = (int)(1+Math.random()*(n));
            if( control[i] != random  ){
                control[i]= random;
                miX += s.charAt(random);
        }
        }
        return miX;
    }

【问题讨论】:

  • 好的,谢谢。但是我可以用这个代码写同样的东西吗?
  • 是的,您的代码看起来会有所不同。请参阅public static String mix(String s) { StringBuilder sb = new StringBuilder(s); Random r = new Random(); for (int i = 0; i &lt; s.length(); i++) { char curr = sb.charAt(i); //current char int rix = r.nextInt(s.length()); //random index char temp = sb.charAt(rix); //memorize char at index rix sb.setCharAt(rix, curr); //put current char to rix index sb.setCharAt(i , temp); //put memorized char to i index } return sb.toString(); }System.out.println(mix("Hello")); 通话:)

标签: java eclipse string methods char


【解决方案1】:

您应该在字符串长度内随机选择两个位置,然后交换它们。 根据您希望字符串中的随机性数量,在循环中运行任意次数。

根据您的代码,如果随机选择两次相同的索引,您可能会错过新字符串中的某些字符

【讨论】:

    【解决方案2】:

    您可以使用 Collections.shuffle。

    String word= "word";
    ArrayList<Character> chars =newArrayList<Character>(word.length());
    
    for(char c : word.toCharArray()){
    chars.add(c); }
    
    Collections.shuffle(chars);
    char[] shuffled =newchar[chars.size()];
    
    for(int i =0; i < shuffled.length; i++){
     shuffled[i]= chars.get(i);
    }
    
    String shuffledWord =newString(shuffled);
    

    与您的代码类似但不使用函数的另一种方式是:

    public static void main(String[] args) {
    
    // Create a random object
    Random r = new Random();
    
    String word = "Animals";
    
    System.out.println("Before: " + word );
    word = scramble( r, word );
    System.out.println("After : " + word );
    }
      public static String scramble( Random random, String inputString )
    {
        // Convert your string into a simple char array:
        char a[] = inputString.toCharArray();
    
        // Scramble the letters
    
        for( int i=0 ; i<a.length-1 ; i++ )
       {
            int j = random.nextInt(a.length-1);
            // Swap letters
           char temp = a[i]; a[i] = a[j];  a[j] = temp;
      }       
    
        return new String( a );
     }
    

    【讨论】:

      【解决方案3】:

      将字符串字符与此代码混合

      import java.util.*;
      
      class MixStringChars
      {
          public static String mix(String s)
          {
              StringBuilder sb = new StringBuilder(s);
              Random r = new Random();
              for (int i = 0; i < s.length(); i++)
              {
                  char curr = sb.charAt(i);          //current char
                  int  rix  = r.nextInt(s.length()); //random index
                  char temp = sb.charAt(rix);        //memorize char at index rix
                  sb.setCharAt(rix, curr);           //put current char to rix index
                  sb.setCharAt(i  , temp);           //put memorized char to i index
              }
              return sb.toString();
          }
      
          public static void main (String[] args)
          {
              System.out.println(mix("Hello"));
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多