jeudi 11 mars 2021

How do I make an array of random strings from another array in Java? [duplicate]

I'm trying to make a program similar to what is shown on cstimer.net, a random list of moves to scramble a Rubik's Cube. This is my code so far:

import java.util.Random;
class Main {
public static void main(String[] args) {
 /* F (front): the face facing the solver.
B (back): the back face.
R (right): the right face.
L (left): the left face.
U (up): the upper face.
D (down): the face opposite to the upper face.
*/ 
Random rand = new Random();
String[] moves = {"F", "F'", "F2", "B", "B'", "B2", "R", "R'", "R2", "L", "L'", "L2", "U", "U'", "U2", "D", "D'", "D2"};

int index = rand.nextInt(moves.length);

How do I make a new array with a random combination of 20 elements in that array? With duplicates of course, since the original only has 15 elements.

I tried concatenating strings of moves[index] together, but they were all the same.

String index_1 = (moves[index] + " ");
String index_2 = (moves[index] + " ");
System.out.println(index_1 + index_2  ); 

Output: U U



Aucun commentaire:

Enregistrer un commentaire