我正在编写一个小程序来在特定时间关闭计算机。虽然有些不对劲
I'm writing a small program to shutdown a computer at a certain time. Somethings wrong though
我的代码如下。它应该是一个简单的代码来检查时间,并将它与我希望 child 的计算机在晚上关闭的两个不同时间进行比较。我对编码有非常基本的理解,因为我没有真正对非常极端的编码做过任何事情。我想我会试一试,但我不完全确定我在代码中做错了什么,我能够完成不同的步骤,直到我到达 if 语句所在的位置:if %yesno% EQU y (
。 ...以及此后的任何事情,它说,出于某种原因,此时不期望 if 语句,然后立即关闭 window。我能够非常快速地截屏。我的所有代码都基于对代码不同部分的多次 google 搜索。如果有人能帮忙,那将是非常有用的。
(截图供参考)
https://i.gyazo.com/1449d3d48279b64411d982d27ec0940a.png
@echo off
:questions
cls
set /p start=[Shutdown time (The hour of shutdown, do not add minutes):]
set /p 1ampm=[am/pm:]
cls
set /p end=[Enter the time you want the computer to be available again for use(The hour of shutdown, do not add minutes):]
set /p 2ampm=[am/pm:]
cls
echo Loading....
PING 1.1.1.1 -n 1 -w 2000 >NUL
cls
set /p yesno=[The time you selected the computer to remain off is from %start% - %end% , is this correct, y/n?]
cls
goto :yn
:yn
if %yesno% EQU y (
goto :1timecorrect
) if %yesno% EQU n (
goto :questions
) else (
goto :questions
)
:1timecorrect
if %1ampm% EQU am (
goto :2timecorrect
) if %1ampm% EQU pm (
set realstart=%start%+12
goto :2timecorrect
) else (
cls
echo you did not enter whether or not the start time is am or pm
goto :questions
)
:2timecorrect
if %2ampm% EQU am (
goto :Begining
) if %2ampm% EQU pm (
set realend=%end%+12
goto :Begining
) else (
cls
echo you did not enter whether or not the start time is am or pm
goto :questions
)
:Begining
set mytime=%time:~0,2%
:Start
if %mytime% GEQ %realstart% (
cls
echo time has expired, time to go to bed.
shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
) else (
if %mytime% LEQ %realend% (
echo time has expired, time to go to bed.
shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
) else (
cls
echo This program is Opperating correctly
PING 1.1.1.1 -n 1 -w 600000 >NUL
goto :Start
)
)
如果 mytime 高于 start,则 if %mytime% GEQ ~start
下的所有内容仅应 运行,因此该行下的所有内容都应放在括号中。此外,如果您的 if
语句有一个 else
,第一个右括号 else
和最后一个左括号需要在同一行上。
@echo off
set start=11
set end=19
set mytime=%time:~0,2%
:Start
if %mytime% GEQ %start% (
if %mytime% LEQ %end% (
shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
) else (
echo This program is Operating correctly
sleep 1800
goto :Start
)
)
我的代码如下。它应该是一个简单的代码来检查时间,并将它与我希望 child 的计算机在晚上关闭的两个不同时间进行比较。我对编码有非常基本的理解,因为我没有真正对非常极端的编码做过任何事情。我想我会试一试,但我不完全确定我在代码中做错了什么,我能够完成不同的步骤,直到我到达 if 语句所在的位置:if %yesno% EQU y (
。 ...以及此后的任何事情,它说,出于某种原因,此时不期望 if 语句,然后立即关闭 window。我能够非常快速地截屏。我的所有代码都基于对代码不同部分的多次 google 搜索。如果有人能帮忙,那将是非常有用的。
(截图供参考) https://i.gyazo.com/1449d3d48279b64411d982d27ec0940a.png
@echo off
:questions
cls
set /p start=[Shutdown time (The hour of shutdown, do not add minutes):]
set /p 1ampm=[am/pm:]
cls
set /p end=[Enter the time you want the computer to be available again for use(The hour of shutdown, do not add minutes):]
set /p 2ampm=[am/pm:]
cls
echo Loading....
PING 1.1.1.1 -n 1 -w 2000 >NUL
cls
set /p yesno=[The time you selected the computer to remain off is from %start% - %end% , is this correct, y/n?]
cls
goto :yn
:yn
if %yesno% EQU y (
goto :1timecorrect
) if %yesno% EQU n (
goto :questions
) else (
goto :questions
)
:1timecorrect
if %1ampm% EQU am (
goto :2timecorrect
) if %1ampm% EQU pm (
set realstart=%start%+12
goto :2timecorrect
) else (
cls
echo you did not enter whether or not the start time is am or pm
goto :questions
)
:2timecorrect
if %2ampm% EQU am (
goto :Begining
) if %2ampm% EQU pm (
set realend=%end%+12
goto :Begining
) else (
cls
echo you did not enter whether or not the start time is am or pm
goto :questions
)
:Begining
set mytime=%time:~0,2%
:Start
if %mytime% GEQ %realstart% (
cls
echo time has expired, time to go to bed.
shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
) else (
if %mytime% LEQ %realend% (
echo time has expired, time to go to bed.
shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
) else (
cls
echo This program is Opperating correctly
PING 1.1.1.1 -n 1 -w 600000 >NUL
goto :Start
)
)
如果 mytime 高于 start,则 if %mytime% GEQ ~start
下的所有内容仅应 运行,因此该行下的所有内容都应放在括号中。此外,如果您的 if
语句有一个 else
,第一个右括号 else
和最后一个左括号需要在同一行上。
@echo off
set start=11
set end=19
set mytime=%time:~0,2%
:Start
if %mytime% GEQ %start% (
if %mytime% LEQ %end% (
shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
) else (
echo This program is Operating correctly
sleep 1800
goto :Start
)
)