+ 1
where is bool ,,, //output case x>800 can't change bool to int (error)(•_•)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main() { int x=1; int num = 1; while(num < 6) { x++ ; Console.WriteLine(x); switch (x){ case x>800: num =+1; break ; } } } }
11 Antworten
+ 8
The problem is, you cannot do so, and you don't need to. You just need an if statement, not switch. Your switch block cannot evaluated a ranged condition. (at least, not here)
+ 7
Just this then:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main()
{
int x=1, num=1;
while(num < 6)
{
x++;
Console.WriteLine(x);
if (x>800) num +=1;
}
}
}
}
+ 6
I'm not able to make any sense out of what you just posted. I merely replaced your switch block with an if statement.
+ 3
link to the code:
https://code.sololearn.com/cXe7BXr49hWQ/?ref=app
Problem arise at line
case x>800: num =+ 1;
x>800 is false (false is a value of bool data type), so the line becomes
case false: num =+ 1;
but x is int data type, so the error is you assign a bool case for an int variable.
Kindly read through some notes on switch and operators to understand the code.
+ 1
sorry its not what i need :(
+ 1
thanks now i understand but how to solve these problem, but how to change it from false to int .. or if i can put
(if ) inside the case !!
+ 1
i was mine the switch not while?
+ 1
my question was for switch not for while , how can I add if for case as (var) in
switch
+ 1
thanks
0
Why don't you use a simple "if" instead of a whole switch for one case?
You can't put an arbitrary boolean expression into a case, thats why it is not working.