day05-java-(method, guess character mini-game)
1. Method:
1) for encapsulating a specific logic function
2) The method should be as independent as possible, only to do one thing
3) methods can be repeatedly called multiple times
4) Avoid code duplication, facilitate the maintenance of code, facilitate the team's collaborative development
2. Definition of the method:
Modifier return value type method name (parameter list) {
Method body
}
3. Invocation of the method:
1) No return value: Method name (with reference to the parameter);
2) There is a return value: Data type variable = method name (with reference to the parameter);
Method name (with reference to the parameter);------Not recommended
Usage of 4.return:
1) return value; 1. Ending the execution of the method 2. Returns the result to the caller
2) return; 1. Closing the execution of the method
Guess character Games:
I. Design data structure:
1) char[] CHS; Random character Array
2) char[] input; An array of characters entered by the user
3) int[] result; The results of the comparison
4) int score; Score
int count; Wrong number of guesses
Two. Design program structure: Methods
1) Main method:
public static void Main (string[] args) {
//...
}
2) generate a random character array:
public static char[] Generate () {
char[] CHS = new CHAR[5];
//...
return CHS;
}
3) Contrast: Random character array with user-entered character array
public static int[] Check (char[] chs,char[] input) {
Int[] result = new INT[2];
//...
return result;
}
Three. Design algorithm: Method body
String str = "ABCDE";
1) char[] A = Str.tochararray (); Convert to character array
2) str = Str.touppercase (); Convert to uppercase
str = Str.tolowercase (); Convert to lowercase letter
3) if (Str.equals ("ABCDE")) {//Determine if STR content is ABCDE
}
Implement code three block functionality:
1. Get a randomly generated array of characters CHS
2. Let the user guess, receive the user input character array input
3. Compare CHS with input to get results and show results
If not, repeat the 2nd step.
Method can have a return value or it can have no return value
No return value----return value type void
There is a return value----The return value type written in the exact data type
System.out.println ("HelloWorld");
System.arraycopy (a,1,a1,0,4);
Arrays.sort (arr); No return value
int a = Scan.nextint ();
Double b = scan.nextdouble ();
Double c = math.random ();
Double d = math.sqrt (25);
int[] A1 = arrays.copyof (a,6); has a return value
Prime number = Number of primes : Only 1 is divisible by itself
is a prime number?-------the remainder is not 0
Not prime?-----just have 0.
5 is prime number
5%2/3/4--------------are not allowed 0
7 is prime number
7%2/3/4/5/6----------are not allowed 0
8 Not prime
8%2/3/4/5/6/7--------have 0
9 Not prime
9%2/3/4/5/6/7/8------have 0
Square Root:
sqrt ()------who wants the square root of who to ask for the square root
The square root of 100 is 10
The square root of 25 is 5
The square root of 81 is 9
Demonstration of the method:
Packageday06;//demonstration of the method Public classMethoddemo { Public Static voidMain (string[] args) {//say (); //Sayhi ();//compile error, parameter must be passed//Sayhi (+);//compilation errors, parameter types do not match//sayhi ("Zhangsan");//String name= "Zhangsan"//Sayhi ("Lisi");//String name= "Lisi"//sayhi ("Wangwu");//String name= "Wangwu"//int a = Getnum ();//The value of Getnum () is the value after return//System.out.println (a); //Double b = plus (5.0,6.0);//Double num1=5.5,double num2=6.0//System.out.println (b);//11.5 Doublec=5.5,d=6.0; DoubleE = Plus (c,d);//Double num1=5.5,double num2=6.0//a ();//nested calls to methodsSystem.out.println ("Over"); } //have a parameter return value Public Static DoublePlusDoubleNUM1,Doublenum2) { Doublenum = num1+num2; returnNum//The number in num is returned.//return num1+num2; } //no parameter has a return value Public Static intGetnum () {//return;//compilation error, a value must be returned//return 8.88;//compile error, return value type must match return88;//1. Ending the execution of the method 2. Returns the result to the caller } //no return value with parameter Public Static voidSayhi (String name) {System.out.println ("Hello, my Name" +name); return;//1. Closing the execution of the method } //No parameter no return value Public Static voidsay () {System.out.println ("Hello, my name Freddy."); } Public Static voidA () {System.out.println (111); b (); System.out.println (222); } Public Static voidB () {System.out.println (333); }}
Guess character Games:
Packageday06;ImportJava.util.Scanner;//Guess character Games Public classguessing {//Main Method Public Static voidMain (string[] args) {Scanner scan=NewScanner (system.in); Char[] CHS = Generate ();//get an array of random charactersSystem.out.println (CHS);//Cheating intCount = 0;//wrong number of guesses while(true){//self-made cycle of deathSYSTEM.OUT.PRINTLN ("Guess!")); String Str= Scan.next (). toUpperCase ();//gets the user-entered string and converts it to an uppercase letter if(Str.equals ("EXIT")) {//determine if the contents of STR are exitSystem.out.println ("The next time you come again!")); Break; } Char[] input = Str.tochararray ();//Convert a string to a character array int[] result = Check (chs,input);//comparison: Random character arrays and user-entered character arrays if(result[0]==chs.length) {//The number of positions is 5, which means you guessed it. intScore = 100*chs.length-10*count;//one character 100 points, one wrong time deduction 10 pointsSystem.out.println ("Congratulations, you guessed it!") Must be divided into: "+score); Break; }Else{//wrong guess.count++;//number of guesses increased by 1System.out.println ("The number of characters is:" +result[1]+ ", the number of positions is:" +result[0]); } } } //generate random character array Public Static Char[] Generate () {Char[] CHS =New Char[5]; 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 '};//array of random character ranges Boolean[] flags =New Boolean[Letters.length];//switch Array (corresponds to letters array) for(inti=0;i<chs.length;i++) {//iterating through random character arrays intindex; Do{Index= (int) (Math.random () *letters.length);//generate random numbers from 0 to 25} while(flags[index]==true);//The index subscript is regenerated when the switch corresponding to index subscript is true, indicating that it has been saved//when the index subscript corresponds to the beginning of false, indicating that the index subscript is available, the end of the loopChs[i] = Letters[index];//get characters based on index subscript to letters and assign to each element in CHSFlags[index] =true;//Modify the switch corresponding to index subscript to true to indicate that you have saved } returnCHS; } //comparison: Random character arrays and user-entered character arrays Public Static int[] Check (Char[] CHS,Char[] input) { int[] result =New int[2];//0,0---result[0] for position pairs, result[1] for character pairs for(inti=0;i<chs.length;i++) {//iterating through random character arrays for(intj=0;j<input.length;j++) {//iterating through the character array entered by the user if(Chs[i]==input[j]) {//character Pairsresult[1]++;//1 increase in number of characters if(I==J) {//Position Pairsresult[0]++;//1 Increase in the number of positions } Break;//the remaining characters in input are no longer compared } } } returnresult; }}
Day05-java-(method, guess character mini-game)