To generate a random number using a computer:
Random rand = new Random ();//Make a random generator, random(); a random generator seed can be placed inside the parentheses, this seed can only be an integer (int)
int n = rand. Next (10); n represents a random number that is generated between 0-9
Case: A little game to do scissors
0---Scissors 1---stone 2---cloth
Idea: A computer randomly generates a number, and the number of people entering a 0--2
1, if the person out of the punch minus the computer random number is 1 or-2, then the person won
2, if the person out of the punch minus the computer random number is 0, then the draw
3, if the person out of the punch minus the computer random number is-1 or 2, then the computer won
Console.WriteLine ("please take a punch:"); stringHuman =Console.ReadLine (); Random Rand=NewRandom ();//build a random number generator intn = rand. Next (3);//randomly generates an integer less than 3 stringcomputer; intRen; if(n = =0) {Computer="Scissors"; } Else if(n = =1) {Computer="Stone"; } Else{Computer="cloth"; } if(Human = ="Scissors"|| Human = ="Stone"|| Human = ="cloth")//To define the punch of the person, set the 0,1,2 with the computer generated comparison {if(Human = ="Scissors") {ren=0; } Else if(Human = ="Stone") {ren=1; } Else{ren=2; } Console.WriteLine (Human+"VS"+computer); intJieguo = ren-N; if(Jieguo = =1|| Jieguo = =-2) {Console.WriteLine ("brother, you won! "); } Else if(Jieguo = =0) {Console.WriteLine ("A Draw! "); } Else{Console.WriteLine ("you're too much food! "); } } Else{Console.WriteLine ("Roll Duzi! "); }
The scissors of the IF statement