dimanche 13 novembre 2016

Generating 3 random letters (a-f) in Java for Guessing game

Im stuck on an assignment that requires me to make a letter guessing game using loops only (not arrays). Essentially i'm just struggling to figure out how to generate a random 3 letter code (only letters a-f). This is what i have so far for the code building class... import java.util.Random;

public class CodeLetter {
    Random rnd = new Random ();
    char c = (char) (rnd.nextInt(6) + 'a');
    private char letterValue; //a single letter in the code

    public CodeLetter() 
    {
        String alphabet = "ABCDE";

        String pass = "";
        for (int i = 0; i < 3; i++) {
            pass += alphabet.charAt(Random.nextInt(alphabet.length()));//Randomly select a code letter

I have no idea whats going on in the last line and there are definitely some errors as i was just trying to work from what i could fine online. Any help would be greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire