如何结束IF

本文关键字:IF 结束 何结束 | 更新日期: 2023-09-27 18:13:13

我被卡住了…在我的项目中有学分……我的产品都等于信用。所以当买家用完他们所有的积分后,他们就不能再购买了。但它一直在往负的方向走,我不知道怎么阻止它。

代码

private void CmdProgBar_Click(object sender, EventArgs e)
{
    int Credits = Convert.ToInt32(lblcredits.Text);
    if (Credits <= 0)
    {
        MessageBox.Show("You do not have enough credits");
    }
    else
    if (comboBox1.SelectedIndex == 0)
    {
        MessageBox.Show("You've selected Pair");
        Credits -= 1;
        lblcredits.Text = Convert.ToString(Credits);
    }
    if (comboBox1.SelectedIndex == 1)
    {
        MessageBox.Show("You've selected Banana");
        Credits -= 2;
        lblcredits.Text = Convert.ToString(Credits);
    }
    if (comboBox1.SelectedIndex == 2)
    {
        MessageBox.Show("You've selected Apple");
        Credits -= 3;
        lblcredits.Text = Convert.ToString(Credits);
    }
}

如何结束IF

在c#中,如果没有在if, else ifelse语句之后的语句周围放置{},则该条件仅影响下一个语句

你需要用大括号括住else:

int Credits = Convert.ToInt32(lblcredits.Text);
if (Credits <= 0)
{
    MessageBox.Show("You do not have enough credits");
}
else
{
   ...
}

你有几个问题。

  1. 你只检查用户是否有正数的积分。然后根据他们的选择减去任何数字。即使我作为一个用户,只有一个积分,你的代码也可以让我买一个苹果。

  2. 如果你使用这种代码结构,你需要更多的大括号来包装你的语句。

就我个人而言,我建议做一些不同的事情。我至少要介绍一些其他的结构来使一切变得更简单:
var items = new Dictionary<int, Tuple<string, int>>();
items.Add(0, new Tuple("Pair", 1));
items.Add(1, new Tuple("Banana", 2));
items.Add(2, new Tuple("Apple", 3));
var selectedItem = items[comboBox1.SelectedIndex];
if(Credits >= selectedItem.Item2)
{
    MessageBox.Show(string.Format("You selected {0}", selectedItem.Item1));
    Credits -= selectedItem.Item2;
    lblCredits.Text = Credits.ToString();
}

您可以轻松地创建一个命名类来替换Tuple,这将使您的代码更易于阅读。我只是用它来演示数据结构如何降低原始代码的复杂性。

您现在可以在Dictionary中添加任意多的项目,而无需修改if语句。

看起来您需要更正一些代码。下面是我给你的解决方案。我添加评论是为了让你理解我的回答。记住,如果他们有1个积分,那么如果他们选择苹果或香蕉,他们可以得到负积分。如果你需要有足够的积分来购买苹果或香蕉,那么你必须验证他们是否有足够的积分(我在答案后附上了代码)。

private void CmdProgBar_Click(object sender, EventArgs e) //After the buyer has used up all their credits they shouldn't be able to purchase anymore
{
    int Credits = Convert.ToInt32(lblcredits.Text);
    if (Credits <= 0) //IF credits are less than or equal to zero, the show message
    {
        MessageBox.Show("You do not have enough credits");
    }
    else //Meaning more than zero credits, then see what they have selected
    {
        if (comboBox1.SelectedIndex == 0)
        {
            MessageBox.Show("You've selected Pear"); //Correct term from PAIR to PEAR
            Credits -= 1;  //If credits are equal to 1, remaining credits equal 0
        }
        else if (comboBox1.SelectedIndex == 1)
        {
            MessageBox.Show("You've selected Banana");
            Credits -= 2; //If credits are equal to 1, remaining credits equal -1
        }
        else if (comboBox1.SelectedIndex == 2)
        {
            MessageBox.Show("You've selected Apple");
            Credits -= 3; //If credits are equal to 1, remaining credits equal -2
        }
        lblcredits.Text = Convert.ToString(Credits); //Move down because this will always be used after a selection occurs
    }//End else
}

所以,如果你需要检查他们是否有足够的学分为他们所选择的,做以下操作:

private void CmdProgBar_Click(object sender, EventArgs e) //After the buyer has used up all their credits they shouldn't be able to purchase anymore
{
    int Credits = Convert.ToInt32(lblcredits.Text);
    int cost = comboBox1.SelectedIndex + 1; //Assuming cost is equal to selected index plus 1
    if( Credits >= cost)
    {
        if (comboBox1.SelectedIndex == 0)
        {
            MessageBox.Show("You've selected Pear"); //Correct term from PAIR to PEAR
        }
        else if (comboBox1.SelectedIndex == 1)
        {
            MessageBox.Show("You've selected Banana");
        }
        else if (comboBox1.SelectedIndex == 2)
        {
            MessageBox.Show("You've selected Apple");
        }
        Credits -= cost;
        lblcredits.Text = Convert.ToString(Credits); //Move down because this will always be used after a selection occurs
    } //if
    else
    {
        MessageBox.Show("You do not have enough credits");    
    }//End else
}

让我知道你的想法:)希望这对你有帮助!

您的问题是else仅适用于紧随其后的第一个if。试着把它们都用花括号括起来:

if (Credits <= 0)
{
    MessageBox.Show("You do not have enough credits");
}
else
{
    // Your 3 other if statements go here
}

可以使用return语句

private void DoSomething()
{
    if (someCondition)
    {
        // Some code that does stuff here.
        // Exit the method.
        return;
    }
    // If the above condition is true then this code is not hit.
    // ....
    this.DoSomeOtherStuff();
}

或者如果方法有返回值。

private SomeObject DoSomething()
{
    if (someCondition)
    {
        // Some code that does stuff here.
        // Exit the method.
        return null;
    }
    // If the above condition is true then this code is not hit.
    // ....
    this.DoSomeOtherStuff();
}

我认为你最好使用switch语句。

Switch语句和循环使用break语句。

    int Credits = int.Parse(lblcredits.Text);
    if (Credits <= 0)
    {
        MessageBox.Show("You do not have enough credits");
        return;
    }
    var message = string.Empty;
    var value = comboBox1.SelectedIndex;
    switch (value)
    {
        case 0:
            message = "You've selected Pair";
            break;
        case 1:
            message = "You've selected Banana";
            break;
        case 2:
            message = "You've selected Apple";
            break;
        default:
            // Do default behaviour here, or throw an exception.
            throw new InvalidOperationException("Selected Index Invalid");
    }
    MessageBox.Show(message);
    Credits -= (value + 1);
    lblcredits.Text = Credits.ToString();

希望你不介意,但是我也重构了一些东西。忍不住:)