家庭作业问题。我已经构建了一个应用程序,可以让您计算折扣数量。
3个radiobuttons。
rb_korting5 = 5% discount
rb_korting10 = 10% discount
rb_korting15 = 15% discount
如果您年满65岁或以上,您可获得额外的+10折扣。
我需要用else来缩短跟随代码。我已经尝试过,但它一直在弄乱计算。
public void button_Click(object sender, RoutedEventArgs e)
{
DateTime todate = DateTime.Today;
int CurrentYear = todate.Year;
int price = Convert.ToInt32(tb_Price.Text);
int ammount = Convert.ToInt32(tb_Ammount.Text);
double result = Convert.ToDouble(price * ammount);
int dob = Convert.ToInt32(tb_dob.Text);
int age = Convert.ToInt32(CurrentYear - dob);
if ((rbkorting5.IsChecked == true) && ( age >= 65))
{
result = result * 0.05 + 10;
}
if ((rbkorting5.IsChecked == true) && (age < 65))
{
result = result * 0.05;
}
if ((rbkorting10.IsChecked == true) && (age >= 65))
{
result = result * 0.10 + 10;
}
if ((rbkorting15.IsChecked == true) && (age >= 65))
{
result = result * 0.15 + 10;
}
if ((rbkorting10.IsChecked == true) && (age < 65))
{
result = result * 0.10;
}
if ((rbkorting15.IsChecked == true) && (age < 65))
{
result = result * 0.15;
}
lb_result.Content = result;
}
答案 0 :(得分:1)
您可以将年龄> gt = 65的条件分组并计算这些条件的折扣,并在其他部分计算条件年龄的折扣&lt; 65
此外,当您计算折扣时,您使用单独的变量并从总金额中减去该变量。
public void button_Click(object sender, RoutedEventArgs e)
{
DateTime todate = DateTime.Today;
int CurrentYear = todate.Year;
int price = Convert.ToInt32(tb_Price.Text);
int ammount = Convert.ToInt32(tb_Ammount.Text);
int dob = Convert.ToInt32(tb_dob.Text);
int age = Convert.ToInt32(CurrentYear - dob);
double result = Convert.ToDouble(price * ammount);
double discount = 0d;
if (age >= 65)
{
if (rbkorting5.IsChecked)
discount = result * 0.05 + 10;
else if (rbkorting10.IsChecked)
discount = result * 0.10 + 10;
else if (rbkorting15.IsChecked)
discount = result * 0.15 + 10;
}
else
{
if (rbkorting5.IsChecked))
discount = result * 0.05;
else if (rbkorting10.IsChecked)
discount = result * 0.10;
else if (rbkorting15.IsChecked)
discount = result * 0.15;
}
lb_result.Content = result - discount;
}
答案 1 :(得分:1)
public void button_Click(object sender, RoutedEventArgs e)
{
DateTime todate = DateTime.Today;
int CurrentYear = todate.Year;
int price = Convert.ToInt32(tb_Price.Text);
int ammount = Convert.ToInt32(tb_Ammount.Text);
int dob = Convert.ToInt32(tb_dob.Text);
int age = Convert.ToInt32(CurrentYear - dob);
double result = Convert.ToDouble(price * ammount);
lb_result.Content = SubstractDiscount(result);
}
private double SubstractDiscount(double amount)
{
double discount = 1;
double additionalAgeDiscount = 0;
if(rbkorting5.IsChecked)
discount = 0.05;
if(rbkorting10.IsChecked)
discount = 0.10;
if(rbkorting15.IsChecked)
discount = 0.15;
if(age >= 65)
additionalAgeDiscount = 10;
return amount * discount + additionalAgeDiscount;
}
答案 2 :(得分:0)
您可以根据折扣和年龄组织以下代码:
private void button1_Click(object sender, EventArgs e)
{
DateTime todate = DateTime.Today;
int CurrentYear = todate.Year;
int price = Convert.ToInt32(tb_Price.Text);
int ammount = Convert.ToInt32(tb_Ammount.Text);
double result = Convert.ToDouble(price * ammount);
int dob = Convert.ToInt32(tb_dob.Text);
int age = Convert.ToInt32(CurrentYear - dob);
if (rbkorting5.Checked == true) {
if (age >= 65) result = result * 0.05 + 10;
else result = result * 0.05;
}
else if(rbkorting10.Checked == true) {
if (age >= 65) result = result * 0.1 + 10;
else result = result * 0.1;
}
else if(rbkorting15.Checked == true) {
if (age >= 65) result = result * 0.15 + 10;
else result = result * 0.15;
}
lb_result.Content = result; //==> return discount result
}