+ 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 ; } } } }

27th Jan 2018, 3:24 AM
Ahmad Akil
Ahmad Akil - avatar
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)
27th Jan 2018, 1:32 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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; } } } }
27th Jan 2018, 1:22 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
27th Jan 2018, 1:28 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
27th Jan 2018, 12:33 PM
Hanz
Hanz - avatar
+ 1
sorry its not what i need :(
27th Jan 2018, 5:46 AM
Ahmad Akil
Ahmad Akil - avatar
+ 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 !!
27th Jan 2018, 1:12 PM
Ahmad Akil
Ahmad Akil - avatar
+ 1
i was mine the switch not while?
27th Jan 2018, 1:25 PM
Ahmad Akil
Ahmad Akil - avatar
+ 1
my question was for switch not for while , how can I add if for case as (var) in switch
27th Jan 2018, 1:30 PM
Ahmad Akil
Ahmad Akil - avatar
+ 1
thanks
27th Jan 2018, 1:33 PM
Ahmad Akil
Ahmad Akil - avatar
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.
27th Jan 2018, 8:52 AM
Panka
Panka - avatar