Java----guessing character games

Source: Internet
Author: User

Just learned the method, did a small project, guess the character of the game, here and you share a little bit.
We analyze by the effect we want to achieve:

            1 First Command Line window prompts the user to enter the guessing character game and displays the "guess" 2 then the user enters a five-digit string (cannot be duplicated), then the system makes a judgment 3 if the string "exit" exits the game 4 If you do not guess the correct number of characters and guess the number of positions 5 if you guessed right then give the score, and exit the Game 6 points is a character 100 points, guess the wrong time buckle very. Let's analyze: what data is needed for this game: 1.char[] Ch is used to receive system generated random characters 2.char[] input is used to receive user input characters 3.int score is used to receive fractions 4.int count is used to receive the wrong guess Number of times 5.int[] result is used to store the number of guesses and the number of guessed-to position: 1.                            public static char[] Generate () {//used to generate a random character array char[] ch = new CHAR[5];        ... return ch; } 2.public static int[] Check (char[] ch,char[] input) {//used to check the number of guesses and guessed position int[] result = new I    Nt[ch.length];.. return result; The code is as follows: 1.                            public static char[] Generate () {char[] letter = new char[26];//used to hold a-Z character for (int i =0;i<letter.length;i++) {letter[i]= (char) (i+65);//Convert the letter encoding to a character and hold it in the array} int index = 0;                            boolean[] flag = new boolean[letter.length];//used to mark characters that have already been selected char[] ch = new CHAR[5]; while (index<ch.length) {int i = (int) (Math.rando                                                     M () *letter.length), if (Flag[i]) {//exclude characters that have already been selected                                             Contiune;                                             } Ch[index]=letter[i];                                             index++;                            Flag[i]=true;        } return ch;                                } 2.public static in[] Check (char[] ch,char[] input) {int[] result = new INT[2];     for (int i= 0;i<ch.length;i++) {                                       for (int j=0;j<input.length;j++) {                                                                if (Ch[i]==input[j]) {result[0]++;//number of guessed characters                                                                        if (i==j) { The number of result[1]++;//guessed position break;//if the position is judged by phase                                                        The same does not have to scan the following characters}                                }                                            }                                }             return result;  Next, start writing the main function public static void main (string[] agrs) {char[] ch =                                 Generate ();                                 Scanner SC =new Sacnner (system.in); INT CoUNT =0; int score =0; System.out.println ("Welcome to guessing character game");                                                while (true) {System.out.println ("guess")                                                String str = Sc.nextint (). toUpperCase ();//Convert the input string to uppercase if (str.equal ("EXIT")) {System.out.println ("back                                                Out ");                                                char input = str.tochararrays ();//string converted to character array                                                int result = check (ch,input);                                                        if (result[1]==ch.length) {score = 100*ch.length-10*count; System.oUt.println ("Guessed right, scored:" +score) break;                                                            } else{                                                            System.out.println ("Guess the number of characters is" +result[0]+ "guess the number of character positions is" +result[1]);                                                count++; }}} This game can also be small, such as changing the length of the guessing string to change the game difficulty, or by the score to limit the number of game guessing, and so on, there is not much to write , we can try it.

Java----guessing characters games

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.