If 语句不起作用 C#

问题描述 投票:0回答:4

我不确定我是否真的很累并且错过了一些明显的东西,或者我的程序有问题。基本上我的 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;
        }

我传入字符串 0001010110,我得到的输出为:

0 0
0 1
0 1
0 1
1 0

但没有“已激活”或“假”,即使最后一个是“1 0”。如果这是一个愚蠢的问题,再次抱歉,任何见解或帮助将不胜感激。

c#
4个回答
10
投票

您正在将 char 与 int 进行比较。您尝试进行的检查与您想要完成的任务具有完全不同的含义。您需要检查它是否等于“1”或首先将 char 转换为 int,以便可以进行数字比较。

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

2
投票

由于

nextvaluebinary
String
,因此仅当该字符串具有空字符时此比较才会成功,即
'\0'
:

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

看起来您正在寻找零数字字符,所以您应该写

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

0
投票

Equals 与 char 到 int 一起使用。所以这将使用字符代码。

用这个

    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;
    }

0
投票

私有无效分配数据(int代码) { 尝试 { lblmsg.Text = "";

            sql = "select sl,stud_ID,student_name,Gender,DOB,college,active_yn from student_info where sl= '" + code.ToString() + "' ";



            using (MySqlConnection connectionS = new MySqlConnection(Global.GlobalCS.conDatabaseString))
            {
                connectionS.Open();
                using (MySqlCommand command = new MySqlCommand(sql, connectionS))
                {
                    using (MySqlDataReader myReader = command.ExecuteReader())
                    {
                        while(myReader.Read())
                        {
                            lblsl.Text = myReader["sl"].ToString();
                            txtstudentID.Text = myReader["stud_ID"].ToString();
                            txtstudentName.Text = myReader["Student_name"].ToString();
                            txtgender.Text = myReader["gender"].ToString();
                            
                            txtcollegename.Text = myReader["College"].ToString();
                            rbtnActive.SelectedValue = myReader["active_yn"].ToString();

                            if (myReader["DOB"].ToString() != "")
                           {
                                txtdateofbirth.Text = Convert.ToDateTime(myReader["DOB"]).ToString("yyyy-MM-dd");
                                txtdateofbirth.Enabled = true;
                            }
                            else
                            {
                                txtdateofbirth.Text = "";
                            }
                              txtdateofbirth.Text = myReader["DOB"].ToString();
                            txtcollegename.Text = myReader["College"].ToString();
                        }
                    }
                }
            }
        }
        catch(Exception ex)
        {
            ShowMsgBox.Show("error in assign box");
            Global.GlobalCS.LogError(ex);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.