我有IF语句的问题 [英] I have problem with IF statement

查看:69
本文介绍了我有IF语句的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用int变量和字符串变量的条件

它会出错



请帮帮我



Ajith



我尝试过:



When I try condition with int variable and string variable
it gives an error

Please help me

Ajith

What I have tried:

int nSet =0 ;
int nCount=0 ;
string  cPrevtag ="";

/* nCount and cPrevtag change with my method calling

if (nCount == 1 && cPrevtag = 'O')
   {
     nSet = nSet + 1;

   }

推荐答案

这里有两件事:

Two things wrong here:
if (nCount == 1 && cPrevtag = 'O')



1)cPrevtag被声明为字符串,但你的比较是用一个字符 - 这是不允许的。

2)=是一个赋值,而不是一个比较:C#需要布尔结果(不像C和C ++那样使用非) -Zero-是真)。比较运算符是==



试试这个:


1) cPrevtag is declared as a string, but your comparison is with a character - that isn;t allowed.
2) "=" is an assignment, not a comparison: and C# requires boolean results (unlike C and C++ which work with "non-zero-is-true"). The comparison operator is "=="

Try this:

if (nCount == 1 && cPrevtag == "O")


您的代码存在许多问题,但这会编译。不知道它是否有效,因为你没有显示你的应用程序的其余部分:



There are numerous problems with your code, but this will compile. No idea if it works, since you don't show the rest of your app:

int nSet =0 ;
int nCount=0 ;
string cPrevtag ="";

/* nCount and cPrevtag change with my method calling */

if (nCount == 1 && cPrevtag == "O")
{
    nSet = nSet + 1;

}





正如其他人所说,你无法将字符串与a进行比较字符(单引号表示一个字符)。此外,您必须使用==进行比较。最后,您的评论未被关闭* /



Matt



As others have noted, you can't compare a string to a character ( single quotes indicate a character). Also, you have to use == for comparison. Finally, your comment wasn't closed */

Matt


这篇关于我有IF语句的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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