Made a batch of digital rain, old topic
Mainly for beginners to see
Mainly used to understand the knowledge of batch processing
1. Use of random variables
2. The superposition of variables
The following is the source code:
Copy Code code 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%" = "%echos%&&goto:start" Echo
Goto:num
Let me explain it in detail.
Copy Code code as follows:
@echo off
:: Turn off Echo (needless to say)
Color 0a
The meaning of this sentence is to change the font color of the batch, 0a is the color code (specific can see color command)
: Start
Set num=0
:: Define (redefine) the variable of num, with a value of 0 (we use this variable to compute the number of batch loops)
Set "echos="
:: Define (redefine) a echos variable with a value of one space
: num
set/a a1=%random%%%3
:: This sentence is very important, this sentence can be said to be the core of the digital rain Code
: The meaning of this sentence is to define a random number, the value below "3"
:: To limit the value of random numbers, you must use the/a switch of the SET command
:: For example, I want to define a variable of num, which is a random number, the tangent must be less than 20, the code is
:: set/a num=%random%%%20
:: Above is the complete code, if the/a switch is not written with the set num=%random%%%20
:: Then there will be an error
If "%a1%" = = "1" Set "a1="
: This means that if the variable A1 (that is, the random variable of the previous sentence) is 1, then the value of A1 is a space
::P s (I need to say here)
:: Our program is to simulate the digital rain
:: We may have seen digital rain, digital rain is not all 0 and 1, there are gaps in the middle, this sentence is to achieve this goal
:: The following sentence is the same purpose
:: Finally there is a point to mention
:: I don't know what we found before the statement I didn't use double quotes like set num=0
:: And this set "a1=" but uses double quotes, this is why?
:: In fact, the previous statement set Num=0, can also be changed to set "Num=0"
:: This seems to be relatively standard, is not set "a1=" can also be changed to set a1=?
:: The answer is "No"
:: Because set a1= this sentence, even if you add a space after the statement, batch processing will not identify
:: The value of the A1 is null, and the value of the variable cannot be empty in the batch, so we use a space instead
:: Written as set "a1="
If "%a1%" = = "2" Set "a1="
:: If the A1 value is 2, then the A1 value is a space
If "%a1%" = = "0" set/a a1=%random%%%2
: This means that if the value of the random variable a1 is 0, then the A1 value will randomly take an integer below 2
: In other words, if the A1 value is 0, then A1 randomly select 0 and 12 numbers
Set "echos=%echos%%a1%"
:: Here is what I call a variable overlay
:: The reason for quoting is because A1 may also be a space
:: Next is echos=%echos%. We're just looking at this.
: The meaning of this sentence is to define echos variable as variable echos and variable A1 value
:: Is it a little dizzy
:: We set the echos variable to be a space at the beginning of the program
:: So this is the definition. Echos is defined as a space + variable A1
:: Is the point clear?
:: Let's use the example.
:: Variable Echos now is a space we use [space] instead
:: Let's assume that the variable A1 is the number 1
:: So this variable is to define echos as [space]+1
:: So after the execution of the statement, the echos value is a space and then the number 1
:: Right ... Is it clear? This is actually a variable superposition.
:: If the statement continues to execute, then the echos value is "[Space]1"?).
:: Then the program executes the A1 value assumption is the number 2
:: Then the set "echos=%echos%%a1%" means
:: Define variable Echos value is "[Space]1" + number 2, which is "[Space]12"
::.....
set/a num=%num%+1
: This means that the value of the variable num plus one (that is, the loop once)
If "%num%" = "%echos%&&goto:start" Echo
:: When the loop executes the above statement 75 times after the display of the superimposed variable echos
:: In other words, loop the above statement 75 times after the display variable echos
Goto:num
:: Jump to label num
Color commands All kinds of colour codes