2012-05-01 48 views
1

高興有一個下拉列表,其中可以選擇數量的項目。 1-8。 對於每件商品,如果他們購買一件商品,則可獲得25%的折扣。如果他們購買2,他們會得到30%3 35%,(因此每次購買物品時都會增加5%)。使用c中的下拉列表計算變量折扣#

這樣做會更容易嗎?我看起來很乏味,你能提供一個exaple代碼請。

這是我,但我將不得不if語句做很多。

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (DropDownList1.SelectedValue == "1") 
    { 
     int test = Convert.ToInt32(DropDownList1.SelectedValue); 
     TextBox1.Text = Convert.ToString(test * (199 * (1 - 0.25))); 
    } 
    else if (DropDownList1.SelectedValue == "2") 
    { 
     int test = Convert.ToInt32(DropDownList1.SelectedValue); 
     TextBox1.Text = Convert.ToString(test * (199 * (1 - 0.30))); 
    } 
    else if (DropDownList1.SelectedValue == "3") 
    { 
     int test = Convert.ToInt32(DropDownList1.SelectedValue); 
     TextBox1.Text = Convert.ToString(test * (199 * (1 - 0.35))); 
    } 
} 

回答

2

您應該選擇指數的工作,因爲它給出了一個數字。

乘這個數字與0.05 (0.35 -0.30 = 0.05, 0.30-0.25 = 0.05)並加入0.25

比方說Selected Index0然後(0 * 0.05) + 0.25 = 0.25

如果1然後(1 * 0.05) + 0.25 = 0.30

等等....

+0

謝謝!得到它了 – stefan

1

試試這個,但要確保仄所選擇的值不爲0

int test = Convert.ToInt32(DropDownList1.SelectedValue); 
if(test!=0) 
{ 
    TextBox1.Text = Convert.ToString(test * (199 * (1 - (0.25+(5*(test-1)))))) 
}