mardi 13 juillet 2021

How to generate/compare 4 random char

I want to generate 4 char. Then I need to compare them to print all possibilities. Here are the consign:
I have to use {'a', 'b', 'c', 'd'}; I can use multiple time the same char, like : {'a', 'a', 'b', 'a'};
But the char b need to ALWAYS be followed by a And finally I need to count all the possibilities printed.

Thanks for your time, here is my code :

This part generates "random" char between 'a' and 'd' but it only writes the first letter and adds another... the problem is in the for loop... I'm sure I can delete this part if I can figure out how to compare the char array

import java.util.Random;

public class Exercise1 {
    public static void main(String[] args) {
        char[] options = {'a','b','c','d'};
        char[] result =  new char[4];
        Random r=new Random();
        for(int i=0;i<result.length;i++){
            result[i]=options[r.nextInt(options.length)];
            System.out.println(result);
  }
 }
}

After I need to compare the char... I feel like I can't use the same logic as numbers... can someone help me? I found this code but I don't really know how to use it properly.

if ((symbol >= 'A' && symbol <= 'Z') || symbol == '?') {
// ...}



Aucun commentaire:

Enregistrer un commentaire