Java implementation guessing Alphabet game

Source: Internet
Author: User

Package Day06;import Java.util.scanner;public class Homework {public static void main (string[] args) {Scanner Scanner = NE W Scanner (system.in); String string = "Guessinggame"; System.out.println (string+) "Welcome to the crossword puzzle!" 500 points, the lowest is divided into 0 points! "); System.out.println (string+ "Enter game level (5,7,9)"); int n = scanner.nextint (); int count = 0; System.out.println (string+ "game begins, please enter the sequence of the" +n+ "Letters you guessed: (Exit-Exit)"); char[] CHS = Generate (n); System.out.println (CHS); while (true) {String s = scanner.next (). Trim (). toUpperCase (); char[] input = S.tochararray (); int[] result = Check (CHS, input), int score = 500-count*10;if (S.equals ("EXIT")) {System.out.println ("Welcome to challenge again!") "); break;} if (score<=0) {score=0;} if (result[1] = = chs.length) {System.out.println ("Congratulations! Guess right! Your score is: "+score); else{count++; System.out.println (string+ "You guessed the" +result[0]+ "character, where the" +result[1]+ "character is in the correct position! (Total number = "+count+", Exit-exit!) ");}}} Declares a method that generates a random array of different characters, n controls the number of generated characters public static char[] Generate (int n) {char[] chs = new char[n];char[] letters = {' 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 ' };//set Switch boolean[] flags = new Boolean[letters.length];for (int i=0;i<chs.length;i++) {int index;//letters subscript do{// Generate random Subscript index = (int) (Math.random () *letters.length);} while (Flags[index]);//Is true, then to exist, loop again, get different characters chs[i] = letters[index];//assigns a new array to generate flags[index] = true;//is set to False, indicating that the location already has data}return CHS; Declares a method that compares a randomly generated array with an array of user input, returns an array of int, which holds the number of pairs and the number of position pairs in the comparison after public static int[] Check (char[] chs,char[] input) {int[] result = new Int[2];for (int i=0;i<chs.length;i++) {for (int k=0;k<input.length;k++) {if (chs[i] = = Input[k]) {result [0]++;//record the number of pairs of characters if (i==k) {result[1]++;//record position pairs of}break;}}} return result;}}

Java implementation guessing Alphabet game

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.