C#模数导致程序出现问题 [英] C# Modulus causing issues in program

查看:72
本文介绍了C#模数导致程序出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





Relevent misc info:Project = Tic Tac Toe(控制台应用程序)

我的问题如下:

我有一个最初填充0的数组,必须保持0为0,除非用户移动(然后它将变为1)或计算机移动然后它变为2。 />


最初,如果一行/列/对角线为空(3 0),那么如果你进行计算0 + 0 + 0%2,你会发现它等于0与我的代码冲突..



所以我建议只确保三个collumns / rows / diags都不是0但是这导致计算机不写如果它们都是0,则得到列/行/诊断。

代码示例:

  else   if (posStatus [ 2 ]!=  0 && posStatus [ 5 ]!=  0 && posStatus [ 8 ]!=  0 && (posStatus [ 2 ] + posStatus [ 5 ] + posStatus [ 8 ]% 2  ==  0 ))
{
if (posStatus [ 2 ] == 0
{
posStatus [ 2 ] = 2 ;
return ;
}
else if (posStatus [ 5 ] == 0
{
posStatus [ 5 ] = 2 ;
return ;
}
else if (posStatus [ 8 ] == 0
{
posStatus [ 8 ] = 2 ;
return ;
}



我相信如果最初的if else条件被更改,我的问题将得到解决,但我不知道如何。



提前致谢。

解决方案

一个基本错误:



Quote:

else if(posStatus [2]!= 0&& posStatus [5]!= 0&& posStatus [8]!= 0&&

(posStatus [2] + posStatus [5] + posStatus [8]%2 == 0))





如果posStatus [2]!= 0且posStatus [2]> 0则则无法获得

posStatus [2] + STG == 0除非STG< 0



因此,如果您的情况下情况永远不会成立。



退房在哪里根据需要放置括号和运算符优先级。



http://msdn.microsoft.com/en-us/library/aa691323%28v= vs.71%29.aspx [ ^ ]



[根据要求更新]

附有模数运算符的括号,因为它的如果(posStatus [2]!= 0&& ;; posStatus [5]!= 0&& posStatus [8]!= 0&& (posStatus [2] + posStatus [5] + posStatus [8])%2 == 0)


Hi,

Relevent misc info: Project = Tic Tac Toe (console app)
My issue is the following:
I have an array that initially is filled with 0's which MUST stay as 0's unless the user takes a move (then it'll become a 1) or the computer makes a move then it becomes a 2.

Originally, if one row/collumn/diagonal was empty (3 0's) then if you do the calculation 0 + 0 + 0 % 2 you'll discover it equals 0 which conflicts with my code..

So I was recommended to just make sure three collumns/rows/diags are not all at 0 but this causes the computer not to write a result in the column/row/diag if they are all 0.
Example of code:

else if (posStatus[2] != 0 && posStatus[5] != 0 && posStatus[8] != 0 && (posStatus[2] + posStatus[5] + posStatus[8] % 2 == 0))
            {
                if (posStatus[2] == 0)
                {
                    posStatus[2] = 2;
                    return;
                }
                else if (posStatus[5] == 0)
                {
                    posStatus[5] = 2;
                    return;
                }
                else if (posStatus[8] == 0)
                {
                    posStatus[8] = 2;
                    return;
                }


I believe that if the initial if else condition is changed my issues will be resolved but I'm not sure how to.

Thanks in advance.

解决方案

a basic mistake:

Quote:

else if (posStatus[2] != 0 && posStatus[5] != 0 && posStatus[8] != 0 &&
(posStatus[2] + posStatus[5] + posStatus[8] % 2 == 0))



if posStatus[2] != 0 and posStatus[2]>0 then it is not possible to have
posStatus[2]+ STG ==0 unless STG<0

therefore else if condition will never be true in your case.

check out where to put your parenthesis and operator precedence as you need.

http://msdn.microsoft.com/en-us/library/aa691323%28v=vs.71%29.aspx[^]

[update on request]
enclose with parenthesis for modulus operator since its precedence higher than addition.

else if (posStatus[2] != 0 && posStatus[5] != 0 && posStatus[8] != 0 && (posStatus[2] + posStatus[5] + posStatus[8]) % 2 == 0)


这篇关于C#模数导致程序出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆