简体   繁体   English

单选按钮的C#折扣

[英]C# discount on radio buttons

I need to create a membership that gives the user a series of options to choose from. 我需要创建一个成员资格,为用户提供一系列选项供您选择。 One of the options gives them either a discount of their overall payment cost of either 0%, 2%, or 5%. 一种选择是给他们总付款成本的0%,2%或5%折扣。 For some reason my C# code is putting 0% into the overall discount textbox. 由于某种原因,我的C#代码将0%放入整体折扣文本框中。

C#: C#:

Int discount = 0;
If(basicradiobt.checked)
 Discount *= 0;
If (regularradiobt.checked)
Discount *=2;
If (premiumradiobt.checked) 
Discount *=5;
Totaldiscounttxtbx.text = discount.tosting();

You have two mistakes: 您有两个错误:

  1. You declare the variable discount , then you use it as Discount . 您声明变量discount ,那你就用它作为Discount Because C# is case-sensitive, they are treated as two separate variables; 由于C#区分大小写,因此将它们视为两个单独的变量;
  2. You declare the variable discount , initializing it with 0. Suppose you fixed the mistake above, you multiply its value (0) with 0, 2 or 5, respectively. 您声明变量discount ,将其初始化为0。假设您已解决上述错误,则将其值(0)分别乘以0、2或5。 Any number times 0 equals 0. 任何数字乘以0等于0。

And I guess that in the line 我想这句话

Totaldiscounttxtbx.text = discount.tosting();

you actually meant 你的意思是

Totaldiscounttxtbx.text = discount.tostring();

Later edit, taking into consideration your comments : if you just want to display the discount value, you could do something like this: 稍后进行编辑,并考虑您的评论 :如果只想显示折扣值,则可以执行以下操作:

int discount;
if (basicradiobt.Checked) discount=0;
if (regularradiobt.Checked) discount=2;
if (premiumradiobt.Checked) discount=5;
totaldiscounttxtbx.text=discount.tostring();

If you're interested in the final price, after the discount was applied , try this: 如果您对最终价格感兴趣,请在应用折扣后尝试以下操作:

float startingprice;
if (basicradiobt.Checked) startingprice*=1;;
if (regularradiobt.Checked) startingprice*=0.98;
if (premiumradiobt.Checked) startingprice*=0.95;
totaldiscounttxtbx.text=startingprice.tostring();

(Because I typed fast, I didn't bother with case-sensitivity. But I don't have a compiler on my back either.) (因为我键入速度很快,所以我不关心大小写敏感。但是我也不支持编译器。)

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

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