if +%的问题。 [英] problem with if + %.

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

问题描述

x = 32154

if(X%2)



错误无法将类型''int''隐式转换为''bool ''

x = 32154
if (X % 2)

Error Cannot implicitly convert type ''int'' to ''bool''

推荐答案

是的,在c#中,整数值表达式 0隐式转换为bool == false 0!= true (就像c / c ++那样)。

通常,你会进行一些比较关于价值:

Yes, in c#, an integer valued expression will not be implicitly converted to bool with 0 == false, 0 != true (like c/c++ does).
Typically, you perform some comparison on the value:
int x = 32154;
if ((x % 2) == 0)



如果你只是检查偶数与奇数,只需检查低位(假设你是两个赞美的)架构):


If you are just checking for even vs. odd, just check the low order bit (assuming you''re on a twos-compliment architecture):

if ((x & 1) == 0)    // x is even
if ((x & 1) == 1)    // x is odd


我一直在考虑



if((x%2)== 0)



和我想要的是



if((x%2)== 1)



但tnx无论如何我很欣赏
i have been experamenting with

if ((x % 2) == 0)

and the ting i wanted is

if ((x % 2) == 1)

but tnx anyway i appreciate it


嗯......你们但它疯狂地看着这个!我正在使用C#!



hmm... yes but it is buging like crazy look at this !i am using C#!

Console.WriteLine("This is a program that calculates the gcd of some typed number");
            int StartingNum = Convert.ToInt16(Console.ReadLine());
            int NumForCalculate = StartingNum;
            string gcd = ("");
            int gathering = 1;
            operation:
            int x = 2;
            operation2:
            if ((NumForCalculate / x) == :see below:)
            {
                if (gcd == "")
                    gcd = ("" + x);
                else
                    gcd = (gcd + "," + x);
                NumForCalculate = (NumForCalculate / x);
                gathering = gathering * x;
                if (NumForCalculate <= 1)
                    goto end;
                else
                    goto operation;
            }
            else
            {
                x++;
                goto operation2;
            }
            end:
            Console.WriteLine("gcd of integers {0} is {1}, and gathering this set of numbers is {2}", StartingNum, gcd, gathering);
            Console.ReadKey();





如果我设置20



如果((NumForCalculate / x)== 1)



那么整数20的gcd是11,并且收集这组数字是11



if((NumForCalculate / x)== 0)



然后gcd的整数20是21,并收集这个一组数字是21



if i set 20

if ((NumForCalculate / x) == 1)

then gcd of integers 20 is 11, and gathering this set of numbers is 11

if ((NumForCalculate / x) == 0)

then gcd of integers 20 is 21, and gathering this set of numbers is 21


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

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