A. bat file that changes the background color according to the number the user entered

Asked

Viewed 2,454 times

0

The code below should change the background color after the user enters a number from 0 to 7, but the program closes right after the user enters the number and does not change the color. I’m using the cmd of Win7.

 @ECHO OFF
 SET /p num=Digite um numero de zero a sete:

IF %num% == 0 (
color 0f
) ELSE (
IF %num% == 1 (
color 1f
) ELSE (
IF %num% == 2 (
color 2f
) ELSE (
IF %num% == 3 (
color 3f
) ELSE (
IF %num% == 4 (
color 4f
) ELSE (
IF %num% == 5 (
color 5f
) ELSE (
IF %num% == 6 (
color 6f
) ELSE (
IF %num% == 7 (
color 7f
) ELSE (
ECHO error.
)
)
pause

2 answers

1


Use that code:

@ECHO OFF
SET /p num=Digite um numero de zero a sete:
IF %num% LEQ 7 (
    color %num%f
) ELSE (
    ECHO error.
)

pause

0

BAT files do not handle parentheses very well, the instruction that IF executes comes from the side of it: IF [condition] [instruction] and not: IF [condition] [instruction]

soon:

@ECHO OFF
SET /p num=Digite um numero de zero a sete:

IF %num% == 0 color 0f
IF %num% == 1 color 1f
IF %num% == 2 color 2f
IF %num% == 3 color 3f
IF %num% == 4 color 4f
IF %num% == 5 color 5f
IF %num% == 6 color 6f
IF %num% == 7 color 7f
pause

Browser other questions tagged

You are not signed in. Login or sign up in order to post.