这是根据良好建议的代码MC ND https://stackoverflow.com/users/2861476/mc-nd and Magoo https://stackoverflow.com/users/2128947/magoo:
@echo off
:main
set location=
set /A num=%random% %% 10
if "%num%"=="0" (
set "location=yahoo.com"
) else if "%num%"=="1" (
set "location=msn.com"
) else (
set "location=google.com"
)
start "Chrome" chrome.exe --new-window "%location%"
timeout /t 30 /nobreak >NUL
goto main
这是遵循建议的批处理代码Ed Heal https://stackoverflow.com/users/892256/ed-heal:
@echo off
:main
set location=
set /A num=%random% %% 10
if "%num%"=="0" set "location=yahoo.com" & goto OpenSite
if "%num%"=="1" set "location=msn.com" & goto OpenSite
set "location=google.com"
:OpenSite
start "Chrome" chrome.exe --new-window "%location%"
timeout /t 30 /nobreak >NUL
goto main
See 使用 Windows 批处理文件的单行多个命令 https://stackoverflow.com/q/25343351代码的解释set "location=..." & goto OpenSite
.
此额外信息适用于鲁迪奇安奥蒂 https://stackoverflow.com/users/3837788/rudicangiotti因为他在下面的回答中发表了评论Middas https://stackoverflow.com/users/1309672/middas.
没有必要if
and else
只需一个命令即可使用括号。
因此代码块
if "%num%"=="0" (
set "location=yahoo.com"
) else if "%num%"=="1" (
set "location=msn.com"
) else (
set "location=google.com"
)
被解析为
if "%num%"=="0" (
set "location=yahoo.com"
) else (
if "%num%"=="1" (
set "location=msn.com"
) else (
set "location=google.com"
)
)
也许用 C/C++ 更容易理解,因为它不需要else
与命令在同一行if
或属于匹配的右括号if
.
完整的可编译C/C++示例代码:
#ifdef __cplusplus
#include <cstdio> /* printf */
#include <cstdlib> /* rand */
#include <cstring> /* strcmp */
#else
#include <stdio.h> /* printf */
#include <stdlib.h> /* rand */
#include <string.h> /* strcmp */
#endif
int main (int argc, char* argv[])
{
const char* sLocation;
int iNum = rand() % 10;
if(iNum == 0) sLocation = "yahoo.com";
else if(iNum == 1) sLocation = "msn.com";
else sLocation = "google.com";
printf("Number is %d and location is \"%s\".\n",iNum,sLocation);
/* Some not really useful code to avoid warnings. */
if(argc > 1)
{
if(!strcmp(argv[1],"/?"))
{
printf("There is no help for this small demo application.\n");
}
}
return 0;
}
同样在 C/C++ 中,没有关键字else if
声明为ElseIf
Visual Basic 中的关键字或#elif
预处理器的指令。
因此上面的条件块也可以写成:
/* Variant 1: Same usage of brackets and indents like in first batch example. */
if(iNum == 0) {
sLocation = "yahoo.com";
} else if(iNum == 1) {
sLocation = "msn.com";
} else {
sLocation = "google.com";
}
/* Variant 2: Same usage of brackets like in first batch example,
but this time with indents as it would be 100% correct according
to processing. It is not possible to use this syntax in batch
files because the second if must be on same line as first else. */
if(iNum == 0) {
sLocation = "yahoo.com";
} else
if(iNum == 1) {
sLocation = "msn.com";
} else {
sLocation = "google.com";
}
/* Variant 3: Same usage of brackets like in second batch example,
but without omitting not necessary brackets for first else block. */
if(iNum == 0) {
sLocation = "yahoo.com";
} else {
if(iNum == 1) {
sLocation = "msn.com";
} else {
sLocation = "google.com";
}
}
/* Variant 4: One more variant not possible in batch file,
but using a very common style for C/C++ programmers. */
if(iNum == 0)
{
sLocation = "yahoo.com";
}
else if(iNum == 1)
{
sLocation = "msn.com";
}
else
{
sLocation = "google.com";
}
/* Variant 5: This is variant 3 in coding style of variant 4. */
if(iNum == 0)
{
sLocation = "yahoo.com";
}
else
{
if(iNum == 1)
{
sLocation = "msn.com";
}
else
{
sLocation = "google.com";
}
}
该代码块可以用 C/C++ 编写,并具有更多变体,考虑到括号和缩进的不同样式。
对于批处理文件编码来说,真正有趣的是变体 1 到 3,其中变体 2 显示了变体 1 如果可以在批处理文件中实现的话,应该是什么样子。但没有人加括号和缩进else if
如变体 3 所示的块以及其他几个块else if
最后插入的块if
and else
到目前为止将位于右侧。