How can I create an 80-character string array with the letters a, b, and c? Each time, the places of these letters a, b and c need to be arranged randomly. For example abcbabcabcababbc ... abcba
String[] str = new String[80];
How can I create an 80-character string array with the letters a, b, and c? Each time, the places of these letters a, b and c need to be arranged randomly. For example abcbabcabcababbc ... abcba
String[] str = new String[80];
You could use the Ascii values. Lower case letters start with 97 (a).
public static void main(String[] args) {
Random r = new Random();
String[] s = new String[80];
for (int i = 0; i < 80; ++i) {
s[i] = "" + ((char) (r.nextInt(3) + 97));
}
Arrays.stream(s).forEach(System.out::print);
}
Alternative solution:
public static void main(String[] args) {
Random r = new Random();
String[] letters = {"a", "b", "c"};
String[] s = new String[80];
for (int i = 0; i < 80; ++i) {
s[i] = letters[r.nextInt(letters.length)];
}
Arrays.stream(s).forEach(System.out::print);
}