C#模数导致程序出现问题

编程入门 行业动态 更新时间:2024-10-26 13:32:04
本文介绍了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 因此,如果您的情况下情况永远不会成立。 退房在哪里根据需要放置括号和运算符优先级。 msdn.microsoft/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. msdn.microsoft/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#模数导致程序出现问题

本文发布于:2023-11-13 09:27:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584004.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模数   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!