Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

java - How to validate input against a randomly generated set of letters. Also, how to institute a rounds system

I'm working on an assignment (very much a beginners assignment)and am looking for some guidance as opposed to the code necessarily although obviously code is extremely helpful.

The assignment is essentially a version of Boggle with preset words (i.e. you're given random letters, you need to make a word, word must come from a short preset list or else you get zero). I have no idea where to start when validating the inputted word to ensure it only uses the randomly generated letters with no duplicates unless there are multiples of the same letter in the set. Here's the code for the random letter generator

Random r = new Random(); char[] real = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

    for(int i=0; i < letters.length; i++) {
      letters[i] = real[r.nextInt(26)];
    }

The game also has a rounds system predicated on the user asking for another round at the completion of the first one. What kind of loop should I use for this. Am extremely used to for loops so that's what I went for but I'm feeling I'm probably wrong here. The rounds are identical so obviously I'd just like to loop over the pre-existing code. This is how I've written the round code so far, want to know what I should be insitituting now before going to deep into a mistake

         letters= Instantiable.getLetters(); //Retrieving encryption
     JOptionPane.showMessageDialog(null, "Your random letters will be displayed" );
     System.out.println(random);



     //Input
     JOptionPane.showMessageDialog(null, "please enter a word" );
     1Answer = JOptionPane.showInputDialog("Player 1, your answer");
        if (p1Answer.matches("^[a-zA-Z]*$")) {
            System.out.println("Player 1's answer is :" + p1Answer);
             }
             else {
                JOptionPane.showMessageDialog(null, "Your answer contains invalid characters.NO SOUP FOR YOU!" );
                }


     2Answer = JOptionPane.showInputDialog("Player 2, your answer");
        if (p2Answer.matches("^[a-zA-Z]*$")) {
            System.out.println("Player 2's answer is :" + p2Answer);
             }
             else {
                 JOptionPane.showMessageDialog(null, "Your answer contains invalid characters.NO SOUP FOR YOU!" );
                }
     



}

}

I'm assuming I'm making a mistake by not slapping this into a do while loop, but honestly extremely unsure because I've mostly been coasting using for loops and thus, at this stage am extremely worried that if I choose the wrong loop at the outset I'll be stuck.

Any help would be very much appreciated. Happy New Year!

question from:https://stackoverflow.com/questions/65642237/how-to-validate-input-against-a-randomly-generated-set-of-letters-also-how-to

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use for loop to iterate over a collection of items or predefined set of values. use while loop to check some condition and proceed for next iteration do-while loop if condition check after executing some statements. or recursion, if that simplifies your iteration logic(also note the performance).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...