G
G
gleb_oj2020-06-05 20:55:38
C++ / C#
gleb_oj, 2020-06-05 20:55:38

How to insert a variable in a case if it gives an error: "A constant value is required"?

int d = Convert.ToInt32(Console.ReadLine());
int g = d % 2;
if (g == 0)
{
for (int s = g; s < 20; s++) ;
switch (s)
{
case s: s = 0;
break;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2020-06-05
@gleb_oj

The switch/case construct implies that the value passed to the switch will be initially known.
In fact, each case is a constant.
Try to rewrite under if / else and you will immediately understand why this happens.
Your code

int d = Convert.ToInt32(Console.ReadLine());
            int g = d % 2;
            if (g == 0)
            {
                for(int s = g; s < 20; s++)
                {
                    if(s) // тут уже не понятно с чем сравнивать вашу s для выполнения условия.
                }
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question