if 语句 - 如何使用 C# 打破 if-else-if

本文关键字:打破 if-else-if 何使用 语句 if | 更新日期: 2023-09-27 17:55:58

我如何打破if-else-if.....为什么它不起作用?它只是检查所有条件而不是执行任务。以下是我的代码。我已经通过断点检查了它移动到所有条件,为什么它在满足正确条件后没有停止。即使它不进入 if 活动,它也只是读取所有条件,最后什么都不做。

private void ShowHash()
    {
        inpic = pb_selected.Image;
        Bitmap image = new Bitmap(inpic);
        byte[] imgBytes = new byte[0];
        imgBytes = (byte[])converter.ConvertTo(image, imgBytes.GetType());
        string hash = ComputeHashCode(imgBytes);
        txt_selectedText.Text = hash;
        GetHash();
    }
private void GetHash()
    {
        if (txt_sel1.Text == null && (txt_sel2.Text == null || txt_sel3.Text == null || txt_sel4.Text == null || txt_sel5.Text == null ))
        {
            txt_sel1.Text = txt_selectedText.Text;
            return;
        }
        else if (txt_sel1.Text != null && (txt_sel2.Text == null || txt_sel3.Text == null || txt_sel4.Text == null || txt_sel5.Text == null))
        {
            txt_sel2.Text = txt_selectedText.Text;
            return;
        }
        else if (txt_sel2.Text != null && (txt_sel3.Text == null || txt_sel4.Text == null || txt_sel5.Text == null))
        {
            txt_sel3.Text = txt_selectedText.Text;
            return;
        }
        else if (txt_sel3.Text != null && (txt_sel4.Text == null || txt_sel5.Text == null))
        {
            txt_sel4.Text = txt_selectedText.Text;
            return;
        }
        else if (txt_sel4.Text != null && (txt_sel5.Text == null))
        {
            txt_sel5.Text = txt_selectedText.Text;
            return;
        }

    }

if 语句 - 如何使用 C# 打破 if-else-if

我强烈怀疑问题是Text属性永远不会null任何txt_sel*。假设这些是 UI 中的文本框,则更有可能的是,如果文本框中没有文本,则 Text 属性将返回 "" 而不是 null 。这是大多数 UI 框架处理空控件的方式。

我还建议先将条件提取到局部变量中:

bool hasSel1 = txt_sel1.Text != "";
bool hasSel2 = txt_sel2.Text != "";
bool hasSel3 = txt_sel3.Text != "";
bool hasSel4 = txt_sel4.Text != "";
bool hasSel5 = txt_sel5.Text != "";
if (!hasSel1 && (!hasSel2 || !hasSel3 || !hasSel4 || !hasSel5)
{
    ...
}

理想情况下,为您的控件提供更有意义的名称 - 就可读性而言,具有相同前缀但随后使用数字后缀的变量集合很少是一个好主意。

原因:

如果这些文本框中没有任何内容,textbox.Text将返回一个空字符串 ( "" ) 而不是 null

溶液:

检查""null

private void GetHash()
{
    if (txt_sel1.Text == "" && (txt_sel2.Text == "" || txt_sel3.Text == "" || txt_sel4.Text == "" || txt_sel5.Text == ""))
    {
        txt_sel1.Text = txt_selectedText.Text;
        return;
    }
    else if (txt_sel1.Text != "" && (txt_sel2.Text == "" || txt_sel3.Text == "" || txt_sel4.Text == "" || txt_sel5.Text == ""))
    {
        txt_sel2.Text = txt_selectedText.Text;
        return;
    }
    ....
    ....

编辑:您不必为布尔变量执行==true。if 语句默认根据 true 检查它。使用!检查false

if (hasValue1 && (hasValue2 || hasValue3 || hasValue4 || hasValue5))
    {
        txt_sel1.Text = txt_selectedText.Text;
        return;
    }
    else if (hasValue2 && (!hasValue1 ||hasValue3 || hasValue4 || hasValue5))
    {                
            txt_sel2.Text = txt_selectedText.Text;
            return;
    }
    else if (hasValue3 && (!hasValue1 || hasValue2 || hasValue4 || hasValue5))
    {               
            txt_sel3.Text = txt_selectedText.Text;
            return;                
    }
    ....
    ....

我认为字符串最好使用内置的null和空格检查功能:

bool hasValue1 = string.IsNullOrWhiteSpace(txt_sel1.Text);
bool hasValue2=  string.IsNullOrWhiteSpace(txt_sel2.Text);
bool hasValue3=  string.IsNullOrWhiteSpace(txt_sel3.Text);
bool hasValue4=  string.IsNullOrWhiteSpace((txt_sel4.Text);
bool hasValue5 =  string.IsNullOrWhiteSpace(txt_sel5.Text);

然后定义这些布尔值的 if 条件。这种情况可以处理意外的空值或空格值。

这是ba,因为它可能发现这些条件中的任何一个都不成立。

如果它能找到一个真实的条件,它将执行该 if 块并且不会事件检查其他条件。

为了补充其他人所说的内容,您确实应该在其中有一个最终的 else 语句来捕获以下问题:

else 
{
  throw new InvalidOperationException("My If Statement is Broken");
}

谢谢大家!我的问题通过更改条件和您建议的更改来解决,以下是代码可能会对像我这样的初学者有所帮助。

 private void GetHash()
    {
        bool hasValue1 =  string.IsNullOrWhiteSpace(txt_sel1.Text);
        bool hasValue2 =  string.IsNullOrWhiteSpace(txt_sel2.Text);
        bool hasValue3 =  string.IsNullOrWhiteSpace(txt_sel3.Text);
        bool hasValue4 =  string.IsNullOrWhiteSpace(txt_sel4.Text);
        bool hasValue5 =  string.IsNullOrWhiteSpace(txt_sel5.Text);
        if (hasValue1 && (hasValue2 || hasValue3 || hasValue4 || hasValue5))
        {
            txt_sel1.Text = txt_selectedText.Text;
            return;
        }
        else if (hasValue2 && (!hasValue1 ||hasValue3 || hasValue4 || hasValue5 ))
        {                
                txt_sel2.Text = txt_selectedText.Text;
                return;
        }
        else if (hasValue3 && (!hasValue1 || !hasValue2 || hasValue4 || hasValue5 ))
        {               
                txt_sel3.Text = txt_selectedText.Text;
                return;                
        }
        else if (hasValue4 && (!hasValue1 || !hasValue2 || !hasValue3 || hasValue5 ))
        {
                txt_sel4.Text = txt_selectedText.Text;
                return;
        }
        else if (hasValue5 && (!hasValue1 || !hasValue2 || !hasValue3 || !hasValue4 ))
        {
            txt_sel5.Text = txt_selectedText.Text;
            return;
        }
        else
        {
            CompareHash();
        }

    }