简体   繁体   English

如何随机化多行中的两个字符?

[英]How to randomize two characters in multiple Lines?

I need a random number of 1-4 "X" while the remaining 6-9 characters are all going to be "O".我需要 1-4 个“X”的随机数,而其余 6-9 个字符都将是“O”。 This entire output needs to be printed into 10 lines within the console.整个输出需要在控制台中打印成 10 行。

What I have so far is that I can generate 6 "O" and a random number from 1-4 of "X".到目前为止,我可以生成 6 个“O”和 1-4 个“X”中的随机数。 However, I have no clue on how to make it happen that the X are randomly spread through the O and that the O will fill up the missing X.但是,我不知道如何使 X 随机分布在 O 中并且 O 将填充缺失的 X。

In other words: If the code generates only 2 "X" I need to have 8 "O", in order to make 10 characters.换句话说:如果代码只生成 2 个“X”,我需要有 8 个“O”才能生成 10 个字符。 Now I also have to print it into 10 lines.现在我还必须将它打印成 10 行。 I happen to be really new to java so the following code is all I have so far.我碰巧对 java 很陌生,所以到目前为止我只有以下代码。

import java.util.Random;
  
class GFG { 
    public static void main(String[] args) 
    { 
        test();
    }

    public static void test() {
        Random r = new Random();
        int i = r.nextInt(4)+1;


        for (int k=5; k<=5; k++) {
            for (int m=0; m<=k; m++) {
                System.out.println("O");
            }
            for (int j=0; j<=i; j++) {
                System.out.println("X");
            }
        }
    }
} 

This method generates one line like the ones you desire :此方法会生成您想要的一行:

public static String randomLine() {
    ArrayList<Character> characters= new ArrayList<>();

    Random random=new Random();
    int k=random.nextInt(4) + 1;
    for (int i = 1; i <= k; i++) {
        characters.add('X');
        
    }
    while(characters.size()<10) {
        characters.add('O');
    }
    Collections.shuffle(characters); //  Shuffling !!!
    String str="";
    
    for(int i=0;i<10;i++)
        str=str+characters.get(i);
    return str;
    
}

You can change the return type to ArrayList<Character> if you want.如果需要,您可以将返回类型更改为ArrayList<Character>
Now, to get 10 of such lines you can run the following code:现在,要获得 10 行这样的行,您可以运行以下代码:

for(int i=0;i<10;i++)
    System.out.println(randomLine());

You may use existing Arrays.shuffle / Collections.shuffle to randomize the array/collection of your data after creating an array/list containing random number of Xs:在创建包含随机数 X 的数组/列表后,您可以使用现有的Arrays.shuffle / Collections.shuffle来随机化数据的数组/集合:

public static List<String> getRandomXOs() {
    Random random = new Random();
        
    int countX = 1 + random.nextInt(4);
    List<String> xos = IntStream.range(0, 10)
                                .mapToObj(i -> i < countX ? "X" : "O")
                                .collect(Collectors.toList());
    Collections.shuffle(xos);

    return xos;
}

// -------
// test
for (int i = 0; i < 5; i++) {
    System.out.println(getRandomXOs());
}

Output (random):输出(随机):

[O, O, O, O, X, X, X, O, O, X]
[X, O, O, O, O, X, X, O, X, O]
[O, O, O, O, O, X, O, O, O, X]
[X, O, O, O, O, O, O, O, O, O]
[O, X, X, O, O, O, O, X, O, O]

Using the Java8 Stream library you can solve your problem more efficiently with less lines of code.使用Java8 Stream库,您可以用更少的代码行更有效地解决您的问题。

This test() function has inputs x which controls how many X are going to be printed ranging from 1 to x , and n which controls how many X or O will be printed in a separate line, as you requested.这个test()函数有输入x ,它控制将要打印的X数量,范围从1x ,而n则控制将根据您的要求在单独的行中打印多少XO。

import java.util.Random;
import java.util.stream.*;

class CFG {
    public static void main(String[] args) {
        test(4, 10);
    }

    public static void test(int x, int n) {
        Random random = new Random();
        int countX = 1 + random.nextInt(x);
        
        String str = random
                .ints(1, n + 1)
                .distinct()
                .limit(n)
                .mapToObj(i -> i <= countX ? "X" : "O")
                .collect(Collectors.joining("\n"));

        System.out.println(str);
    }
}

Random Output:随机输出:

X
O
O
X
O
O
O
O
X
O

Here is an optional code closer to yours这是一个更接近您的可选代码

CODE:代码:

import java.util.Random;
  
class main { 
    public static void main(String[] args) 
    { 
        test();
    }

    public static void test() {
        Random r = new Random();
        int i = r.nextInt(4)+1;

        int countX=0;
        for (int j=0; j<10; j++){
            int p=r.nextInt(2);
            if (p==1) {
                System.out.println("O");
            }           
            else{
              if(countX<i){
              System.out.println("X");
              countX+=1;
              }
              else{
                  System.out.println("O");
              }
            }
            
        }
    }
} 

RANDOM OUTPUT:随机输出:

X
X
X
O
X
O
O
O
O
O

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM