Stack Overflow Asked by Malgosh on March 3, 2021
I need a random number of 1-4 "X" while the remaining 6-9 characters are all going to be "O". This entire output needs to be printed into 10 lines within the console.
What I have so far is that I can generate 6 "O" and a random number from 1-4 of "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.
In other words: If the code generates only 2 "X" I need to have 8 "O", in order to make 10 characters. Now I also have to print it into 10 lines.
I happen to be really new to java so the following code is all I have so far.
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.
Now, to get 10 of such lines you can run the following code:
for(int i=0;i<10;i++)
System.out.println(randomLine());
Correct answer by Omid.N on March 3, 2021
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
Answered by PanosPaP on March 3, 2021
Using the Java8
Stream
library you can solve your problem more efficiently with less lines of code.
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.
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
Answered by solid.py on March 3, 2021
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:
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]
Answered by Alex Rudenko on March 3, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP