Java guessing character game

Source: Internet
Author: User

Package days06;

Demand...... , question, why use Do{}while???
Import Java.util.Scanner;
public class Repeatofgussinggame {

public static void Main (string[] args) {
/* in the main () function, you need to create a dead loop, the purpose is: if the user guessed wrong, guess the right jump (break), wrong repeatedly guess
* Note: Because the user is entering a string, we compare the array in the check method and need to convert the string to a character array
* Add the level, actually increase the difficulty, the more difficult the letter, the number of randomly generated letters added to the variable int parameter value, in making the corresponding judgment and transformation can be
*/
Scanner scan = new Scanner (system.in);
int count = 0;

char[] CHS = Genran (); Receive, Invoke
Debug output
System.out.println ("The game begins! ");
Indicates game level, default is 5
int level = 5;
do {
System.out.println ("Guessinggame> Please enter the game level (5, 7, 9)?" ");
Level = Scan.nextint ();
} while (level! = 5 && level! = 7 && level! = 9);
A string that represents a guess
char[] CHS = Genran (level);
System.out.println (CHS);
while (true) {//self-made loop, wrong always guess, right jump out of the loop,
SYSTEM.OUT.PRINTLN ("Guess! ");
String str = Scan.next (); String to receive input
Char[] Input =str.tochararray ();//Convert to character array, next call comparison method to receive results
int[] result = Check (chs,input);
if (result[1]==chs.length) {//If the position is right
int score = RESULT[0]*100-COUNT*10;
System.out.println ("Congratulations, you guessed it!") Score: "+score);
Break
}else{
System.out.println ("The position of the +result[1]+", the characters to the "+result[0]+"! ");
count++;
}
}
}
Random input
public static char[] Genran (Int. level) {
char[] CHS = new Char[level];
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 '};
Boolean[] flags = new boolean[letters.length];//default false, switch flag
for (int i=0;i<chs.length;i++) {
int index;
do{
index = (int) (Math.random () *letters.length);
}while (flags[index]==true);
Chs[i]= Letters[index];
Flags[index] = true;
}

return CHS;
}
Contrast
public static int[] Check (char[] chs,char[] input) {
Int[] result = new Int[2];//result[1] position, result[0] character pair
for (int i=0;i<chs.length;i++) {//two-layer loop
for (int j=0;j<input.length;j++) {
if (Chs[i]==input[j]) {//Compare characters right
result[0]++;
if (i==j) {//position
result[1]++;
Break
}
}
}
}

return result;
}
}

Java guessing character 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.