A batch processing of digital rain, old questions
For beginners
It is mainly used to learn about batch processing.
1. Use of Random Variables
2. Variable superposition
Below isSource code:CopyCodeThe Code is as follows: @ echo off
Color 0a
: Start
Set num = 0
Set "echos ="
: Num
Set/A a1 = % random % 3
If "% a1 %" = "1" set "a1 ="
If "% a1 %" = "2" set "a1 ="
If "% a1 %" = "0" set/A a1 = % random % 2
Set echos = % echos % a1 %
Set/a num = % num % + 1
If "% num %" = "75" Echo % echos % & Goto: Start
Goto: num
Let me explain in detailCopy codeThe Code is as follows: @ echo off
: Disable echo (needless to say)
Color 0a
: This statement changes the font color of the batch processing. 0a is the color code (see the color command for details)
: Start
Set num = 0
: Defines (redefines) The num variable. The value is 0 (we use this variable to calculate the number of batch processing cycles)
Set "echos ="
: Defines (redefines) the Echos variable. The value is a space.
: Num
Set/A a1 = % random % 3
: This sentence is very important. It can be said to be the core of the entire digital rain code.
: Defines a random number with a value below "3 ".
: To limit the random number value, you must use the/a switch of the SET command.
: For example, if I want to define a num variable, which is a random number and must be smaller than 20, the code is
: Set/a num = % random % 20
: The above is the complete code. If the/a switch is not used, set num = % random % 20 is written.
: An error occurs.
If "% a1 %" = "1" set "a1 ="
: If the A1 variable (that is, the random variable in the previous sentence) is 1, the A1 value is a space.
: PS (it is necessary to talk about it here)
: WeProgramThe purpose is to simulate digital rain
: You may have seen digital rain. It is not all 0 and 1, but there is a gap between them. This sentence is for this purpose.
: The following sentence also serves the same purpose.
: One more point to mention
: I do not know whether we have found any of our previous statements. I do not use double quotation marks, such as set num = 0.
: But this set "a1 =" uses double quotation marks. Why?
: Actually, the previous statement set num = 0 can also be changed to set "num = 0"
: This seems to be a standard. Can it be changed to set a1 = if set "a1 = "?
: The answer is "no"
: Because set a1 =, even if you add spaces after the statement, the batch processing will not recognize it.
: The A1 value is empty. In batch processing, the variable value cannot be empty. Therefore, we use spaces instead.
: Set "a1 ="
If "% a1 %" = "2" set "a1 ="
: If the A1 value is 2, the A1 value is a space.
If "% a1 %" = "0" set/A a1 = % random % 2
: If the random A1 value is 0, the A1 value is a random integer less than 2.
: In other words, if the A1 value is 0, A1 will randomly select the numbers 0 and 1.
Set "echos = % echos % a1 %"
: Here is what I call variable superposition.
: The quotation mark is used because A1 may also be a space.
: The second step is echos = % echos % .. now we only read this section
: This statement defines the Echos variable as the values of the variables echos and A1.
: Is it dizzy?
: We set the Echos variable as a space at the beginning of the program.
: So this time we define echos as a space + variable A1.
: Is it clear?
: Let's use an instance.
: The variable echos is now a space. We use [space] instead.
: Let's assume that the variable A1 is number 1.
: Then this variable defines echos as [space] + 1
: After the statement is executed, the value of echos is a space followed by a number 1.
: Right... clear? This is actually a superposition of variables.
: If the statement continues to be executed, the value of echos is "[space] 1", right?
: Then the program executes A1 again. The value is assumed to be number 2.
: Set "echos = % echos % a1 %" means
: Defines the value of the variable echos as "[space] 1" + number 2, that is, "[space] 12"
::.....
Set/a num = % num % + 1
: The value of the variable num plus one (that is, once in a loop)
If "% num %" = "75" Echo % echos % & Goto: Start
: When the preceding statements are executed cyclically 75 times, the variable echos with a superposition is displayed.
: In other words, the variable echos is displayed 75 times after the preceding statements are cyclically executed.
Goto: num
: Jump to the label num
Color command