简体   繁体   English

C# 演唱会门票,简单的 switch、case 和 if-else

[英]C# concert ticket, simple switch, case, and if-else

Program Specification: The purpose of this application will be to calculate the cost for concert tickets.程序规范:此应用程序的目的是计算音乐会门票的成本。 The individual ticket cost is based on where the tickets are located.个人票价取决于门票所在的位置。 Based on the number of tickets ordered, a percentage off discount will be applied to the total cost.根据订购的门票数量,总费用将应用一定比例的折扣。 Finally, if the customer is a preferred customer, they will receive $10 off their order最后,如果客户是首选客户,他们将获得 10 美元的订单折扣

private void calculateButton_Click(object sender, EventArgs e)
{
    double Box = 75.25;
    double Pavillion = 30.75;
    double Lawn = 21.50;
    double Tickets;
    double discount;
    int Number;

    Number = 0;
    discount = ;

    if (int.TryParse(numberofTicketsTextbox.Text, out Number))
    else MessageBox.Show("Invalid");

    if (boxRadioButton.Checked)
        singleTicketLabel.Text = Box.ToString();

    if (pavilionRadioButton.Checked)
        singleTicketLabel.Text = Pavillion.ToString();

    if (lawnRadioButton.Checked)
        singleTicketLabel.Text = Lawn.ToString();

    switch(discount)
    {
        case_1:

    }
}

I just don't know how to get the Case started to include the discount我只是不知道如何让案例开始包含折扣

Would something like this be useful - adapted straight from the MSDN reference on Switch statements像这样的东西会有用吗 - 直接改编自Switch 语句上MSDN 参考

double discount = 0;
switch (Number)   // <-- AWFUL name for a variable by the way
{
    case 1:       // For one ticket
        discount = 0.9;   // For 10%
        break;
    case 2:       // For two
    case 3:       // or three tickets
        discount = 0.8;   // For 20%
        break;
    default:
        discount = 1;
        break;
}

price -= price_before_discount * discount

Bear in mind as soon as one condition of the switch statement is made then the other options are not assessed and the switch breaks out.请记住,一旦 switch 语句的一个条件成立,则不会评估其他选项,并且 switch 会中断。

As there is already an answer just another hint:因为已经有一个答案只是另一个提示:

You can exchange你可以换

if (int.TryParse(numberofTicketsTextbox.Text, out Number))
else MessageBox.Show("Invalid");

with

if (!int.TryParse(numberofTicketsTextbox.Text, out Number))
   MessageBox.Show("Invalid");

Notice the difference.注意区别。 In your code you had an empty block after the if statement and just used the else block.在您的代码中,您在if语句之后有一个空块,并且只使用了else块。 By negating with !通过否定! you now can use the block of the if statement itself.您现在可以使用if语句本身的块。

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

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