简体   繁体   English

如果声明不起作用C#

[英]If statement not working C#

I'm not sure if I'm just really tired and missing something obvious or there is something wrong with my program. 我不确定我是否真的很累,错过了一些明显的东西,或者我的程序出了什么问题。 Basically my if statement condition is not working. 基本上我的if语句条件不起作用。

public bool check(string nextvaluebinary)
        {
            bool test = true;

            for (int i = -1; i < 8; ++i)
            {
                i++;
                System.Console.WriteLine(nextvaluebinary[i] + " " + nextvaluebinary[i + 1]);
                if (nextvaluebinary[i] == 1)
                {
                    System.Console.WriteLine("Activated");
                    if (nextvaluebinary[i + 1] == 0)
                    {
                        test = false;
                        System.Console.WriteLine("false");
                    }
                }
                else
                {
                    test = true;
                }

                if (test == false)
                {
                    break;
                }
            }

            return test;
        }

I'm passing in the string 0001010110 and im getting an output of: 我正在传递字符串0001010110并获得输出:

0 0
0 1
0 1
0 1
1 0

but no "activated" or "false" even though the last one is "1 0". 但即使最后一个是“1 0”,也没有“激活”或“假”。 Again sorry if this is a dumb question and any insight or help would be greatly appreciated. 再次抱歉,如果这是一个愚蠢的问题,任何见解或帮助将不胜感激。

You're comparing a char against an int. 你将char与int进行比较。 The check you're attempting carries a completely different meaning than what you're trying to accomplish. 你正在尝试的检查带有与你想要完成的完全不同的含义。 You need to either check if its equal to '1' or cast the char to an int first so you can do a numeric comparison. 您需要检查它是否等于'1'或首先将char转换为int,以便进行数值比较。

if (nextvaluebinary[i] == '1')

Since nextvaluebinary is a String , this comparison will succeed only if that string has a null character, ie '\\0' : 由于nextvaluebinary是一个String ,因此只有当该字符串具有空字符,即'\\0' ,此比较才会成功:

if (nextvaluebinary[i + 1] == 0)

It looks like you are looking for a zero digit character, instead, so you should write 看起来你正在寻找一个零字符,所以你应该写

if (nextvaluebinary[i + 1] == '0')

Equals is used with char to int. Equals与char一起使用。 So that will use char code. 所以这将使用char代码。

Use this 用这个

    public static bool check(string nextvaluebinary)
    {
        bool test = true;

        for (int i = -1; i < 8; ++i)
        {
            i++;
            System.Console.WriteLine(nextvaluebinary[i] + " " + nextvaluebinary[i + 1]);
            if (nextvaluebinary[i] == '1')
            {
                System.Console.WriteLine("Activated");
                if (nextvaluebinary[i + 1] == '0')
                {
                    test = false;
                    System.Console.WriteLine("false");
                }
            }
            else
            {
                test = true;
            }

            if (test == false)
            {
                break;
            }
        }

        return test;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM