I am trying to add a number to a list but only if the number is not 255 and not 0 id like to avoid a nested if. The code I have to do this is as follows.
if (!(r == 255 || r == 0))
{
rlist.Add(r);
listBox2.Items.Add(Math.Floor(r).ToString());
}
However I am still getting 255 and 0 added to the listbox and I can not figure out why. Can anyone point out what i am doing wrong?
Thanks in advance for any help.
As others have mentioned, r is floating point. Try this:
var rFloor = Math.Floor(r);
if (!(rFloor == 255 || rFloor == 0))
{
rlist.Add(r); // might want to use rFloor here too
listBox2.Items.Add(rFloor.ToString());
}
The only explanation I see is that r is not an int, but a double or something like that.
So imagine r = 255.3...
if (!(r == 255 || r == 0)) // is true, r is not 255 and not 0
but
listBox2.Items.Add(Math.Floor(r).ToString());
adds "255" as Math.Floor(255.3) returns 255.
Forgot to suggest a solution, TarkaDaal provides one already.
Related
Hello stackoverflow community,
I'm working on a project and am stuck on the following. What I have is a public enum HorizontalDirection class that determines Left or Right. What I need to do is call random.Next(0, 2) with my ternary operator to determine if it is 0 move left else if 1 right. I have not had any luck with any of the following so far.
Enum.Parse(random.Next(0, 2) == 0 ? HorizontalDirection.Left : HorizontalDirection.Right);
(HorizontalDirection)random.Next(0, 2) == 0 ? HorizontalDirection.Left : HorizontalDirection.Right;
To the majority that are more experienced than I am I apologize for taking your time on something that might seem trivial to you. To me it's an opportunity to learn why my logic fails.
Your second option works. Here's a working example.
var l = new List<HorizontalDirection>();
Random r = new Random();
for(int i = 0; i < 1000;i++)
{
var d = r.Next(0, 2) == 0 ? HorizontalDirection.Left : HorizontalDirection.Right;
l.Add(d);
}
Console.WriteLine($"lefts: {l.Count(d => d == HorizontalDirection.Left)}, rights: {l.Count(d => d == HorizontalDirection.Right)}");
Output
lefts: 468, rights: 532
You just need to do a random and cast it to enum:
This is because an int can be casted to an enum (as far as the int o is 0 or 1 !). (myEnum)0 will return the first value of myEnum and so on. Good luck on your project !
Random rd = new Random();
HorizontalDirection direction = (HorizontalDirection) rd.Next(2);
And this is how it would look like with ternary operators:
Random rd = new Random();
var random = rd.Next(2);
HorizontalDirection direction = random == 0 ? HorizontalDirection.Left : TransactionState.Right;
I know this is probably a newbie question, however I need to get a recommendation for the design of this.
I need to evaluate the result of a set of conditions and they must be evaluated irrespective of the outcome of a preceding condition. This brings the case of using regular OR(|) or short-circuit evaluation using ||.
Below is the code that I need to make a design decision about, however the end goal is to be able to evaluate or condition regardless.
private bool checkExistingBPNInSession()
{
var exDirectors = (List<ViewModels.NewContact>)Session["NewDirectorDetails"];
var exTaxConsultant=(List<ViewModels.NewContact>)Session[Resources.Global.NewTaxConsultantDetails];
var exTaRep = (List<ViewModels.NewContact>)Session["NewTaxRepresentativeDetails"];
if (exDirectors.Count() != 0 || exTaRep.Count() != 0 || exTaxConsultant.Count() != 0)
{
var QueryCheckDir = (from x in exDirectors where x.BPN==txtBusinessPartnerIdNumber.Text select x.BPN).ToList();
var QueryCheckTaxConsultant = (from x in exTaxConsultant where x.BPN == txtBusinessPartnerIdNumber.Text select x.BPN).ToList();
var QueryCheckTaxRep = (from x in exTaRep where x.BPN == txtBusinessPartnerIdNumber.Text select x.BPN).ToList();
if (QueryCheckDir.Count() > 0 || QueryCheckTaxConsultant.Count() > 0 || QueryCheckTaxRep.Count() > 0)
{
return true;
}
else
{
return false;
}
}
return false;
}
These parts here have to be evaluated:
exDirectors.Count() != 0 || exTaRep.Count() != 0 || exTaxConsultant.Count() != 0
and this also
QueryCheckDir.Count() > 0 || QueryCheckTaxConsultant.Count() > 0 || QueryCheckTaxRep.Count() > 0
Please, I am seeking the best recommendations.
Thanks guys.
If you want an evaluation done, no matter what, you should use the
|-operator - this will evaluate every condition even if the outcome of the final expression would not change (contrary to ||-operator)
Here is a simple demo using dotnetfiddle
I'm trying to get away with a slick one liner as I feel it is probably possible.
I'll put my code below and then try to explain a little more what I'm trying to achieve.
for (int p = 0; p < 2; p++)
{
foreach (string player in players[p])
{
if (PlayerSkills[player].streak_count *>* 0) //This line
PlayerSkills[player].streak_count++;
else
PlayerSkills[player].streak_count = 0;
}
}
*(p==0 ? >:<) the comparison operator is chosen depending on p.
Of course what I've written is rubbish. But basically I want to use >0 when p==0, and <0 when p>>0. Is there a nice way to achieve this?
Well, you should use what is most readable, even if it is not as consice. That said...
// Invert the count for all but the first player and check for a positive number
if (PlayerSkills[player].streak_count * (p==0 ? 1 : -1) > 0)
I don't know about slick, but the following and/or combination is one line:
if ((p == 0 && PlayerSkills[player].streak_count > 0)
|| PlayerSkills[player].streak_count < 0)
...
This will only ever do the array index once (due to the p==0 condition occurring first) and so is equivalent to the "ternary" you wrote (albeit a bit more verbose).
p > 0 ? whenGreaterThanZero : whenZeroOrLess ;
E.g.
int p = 1; bool test = p > 0 ? true : false ;
Lets test = True
In casino slot games you often have a Wild game piece. What would be a good way of including this mechanic into comparing with 2 other pieces? e.g. given 3 game pieces [Cherry][Cherry][Joker] would be a match.
The code I'm using right now seems really overweight, is there anything that can be done (think bitwise operators?) to make it easier to work with?
if ((box1.BoxRank == box2.BoxRank ||
box1.BoxRank == BoxGameObject.Ranks.Joker ||
box2.BoxRank == BoxGameObject.Ranks.Joker) &&
(box1.BoxRank == box3.BoxRank ||
box1.BoxRank == BoxGameObject.Ranks.Joker ||
box3.BoxRank == BoxGameObject.Ranks.Joker) &&
(box2.BoxRank == box3.BoxRank ||
box2.BoxRank == BoxGameObject.Ranks.Joker ||
box3.BoxRank == BoxGameObject.Ranks.Joker))
{
// has 3 of a kind, or 1 joker and 2 of a kind, or 2 jokers and 1 other
return true;
}
This is easier if you think of the operation in terms of the set of the value of all of the boxes. Just remove all of the jokers from that set and then verify that all of the values are identical:
var allRanks = new[]{box1.BoxRank, box2.BoxRank, box3.BoxRank};
var threeOfAKind = allRanks.Where(rank => rank != BoxGameObject.Ranks.Joker)
.Distinct()
.Count() < 2;
First just remove all of the jokers. If, after doing that, there are two or more distinct values then we do not have a three of a kind.
Yes, represent the Joker as an int with all binary 1's, like 7, 15 or 31.
Represent the cherry and others with an int with only singe binary 1 like 1,2,4,8 smaller than the Joker. Leave zero unused.
Then, using bitwise AND your condition is equivalent to:
(box1.BoxRank & box2.BoxRank & box3.BoxRank) > 0
Note that 3 Jokers will satisfy the condition too.
I have two positions on a 3D system, say [15, 32, 42] and [16, 32, 42]
Is there a easy way to check if they are within a 1 block radius from each other?
This is what I have, but is there a better way of doing it:
if (pos[0] == pos1[0] / 32 || pos[0] == pos1[0] + 1 || pos[0] == pos1[0] - 1)
{
if (pos[1] == pos1[1] || pos[1] == pos1[1] - 1 || pos[1] == pos1[1] + 1)
{
if (pos[2] == pos1[2] || pos[2] == pos1[2] + 1 || pos[2] == pos1[2] - 1)
{
Thanks,
David
You can use Math.abs(pos[0]-pos1[0]) <= 1 to check if two coordinates in the same plane are at most 1 apart.
So all in all, your code could look like this:
if( Math.abs(pos[0]-pos1[0]) <= 1
&& Math.abs(pos[1]-pos1[1]) <= 1
&& Math.abs(pos[2]-pos1[2]) <= 1 )
{
Within a 1 block radius
}
Note that I do not understand why you divided your first equation by 32. I did not include that in this answer.
Note also that this solution makes things a little more readable, but that yours is correct too.
I haven't done this in c# but in Java I use JTS. http://geoapi.codeplex.com/ seems to provice the same functionality in c#. Then you will represent your points as Point objects and have all sorts of useful geospatial functions to use.
But for this case, are you looking for the "as the crow flies" distance, which is just pythagoras, or the "walking distance", which would involve finding the shortest valid route in a directed graph of footpaths?
Julian