2013-04-18 18 views
-4

我已經獲得了我的招聘人員作爲初級開發人員的工作編碼。有三種選擇,我遇到了需要計算購買的問題。除非是書籍,食品或藥品,否則所有物品都應該徵收10%的稅。任何進口產品都要徵收額外5%的稅款,即使他們免稅。因此,我創建了一個表單,允許用戶鍵入物品的名稱,兩個複選框以指明是否已導入或免稅,用於輸入價格的文本框以及每個輸入的文本框。下面是一個應該計算總銷售稅的文本框,下面是一個文本框。第一個複選框名爲「Item1Import」,下一個名爲「Item1Exempt」。價格文本框被命名爲「Item1Price」,另一個「Item1Output」。對於每個項目,數字都會改變,Item2Import,Item3Import等。最後兩個文本框被稱爲「SalesTax」和「Total」。我無法將創建收據的系統放在一起

這是我到目前爲止的代碼。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 


     private void Item1Price_TextChanged(object sender, EventArgs e) 
      { 
       if(Item1Exempt.Checked && Item1Import.Checked) 
       { 
        Item1Output.Text = ((Convert.ToInt32(Item1Price.Text)) + (Convert.ToInt32(Item1Price.Text) * 0.05).ToString("C2")); 
       } 
       else if(Item1Exempt.Checked && !Item1Import.Checked) 
       { 
        Item1Output.Text = (Convert.ToInt32(Item1Price.Text)).ToString("C2"); 
       } 
       else if(!Item1Exempt.Checked && Item1Import.Checked) 
       { 
        Item1Output.Text = ((Convert.ToInt32(Item1Price.Text) + (Convert.ToInt32(Item1Price.Text) * 0.1) + (Convert.ToInt32(Item1Price.Text) * 0.05)).ToString("C2")); 
       } 
       else 
       { 
        Item1Output.Text = ((Convert.ToInt32(Item1Price.Text)) + (Convert.ToInt32(Item1Price.Text) * 0.1)).ToString("C2"); 
       } 

      } 

     private void Item2Price_TextChanged(object sender, EventArgs e) 
      { 
       if (Item2Exempt.Checked && Item2Import.Checked) 
       { 
        Item2Output.Text = ((Convert.ToInt32(Item2Price.Text)) + (Convert.ToInt32(Item2Price.Text) * 0.05).ToString("C2")); 
       } 
       else if (Item2Exempt.Checked && !Item2Import.Checked) 
       { 
        Item2Output.Text = (Convert.ToInt32(Item2Price.Text)).ToString("C2"); 
       } 
       else if (!Item2Exempt.Checked && Item2Import.Checked) 
       { 
        Item2Output.Text = ((Convert.ToInt32(Item2Price.Text) + (Convert.ToInt32(Item2Price.Text) * 0.1) + (Convert.ToInt32(Item2Price.Text) * 0.05)).ToString("C2")); 
       } 
       else 
       { 
        Item2Output.Text = ((Convert.ToInt32(Item2Price.Text)) + (Convert.ToInt32(Item2Price.Text) * 0.1)).ToString("C2"); 
       } 
      } 

     private void Item3Price_TextChanged(object sender, EventArgs e) 
      { 
       if (Item3Exempt.Checked && Item3Import.Checked) 
       { 
        Item3Output.Text = ((Convert.ToInt32(Item3Price.Text)) + (Convert.ToInt32(Item3Price.Text) * 0.05).ToString("C2")); 
       } 
       else if (Item3Exempt.Checked && !Item3Import.Checked) 
       { 
        Item3Output.Text = (Convert.ToInt32(Item3Price.Text)).ToString("C2"); 
       } 
       else if (!Item3Exempt.Checked && Item3Import.Checked) 
       { 
        Item3Output.Text = ((Convert.ToInt32(Item3Price.Text) + (Convert.ToInt32(Item3Price.Text) * 0.1) + (Convert.ToInt32(Item3Price.Text) * 0.05)).ToString("C2")); 
       } 
       else 
       { 
        Item3Output.Text = ((Convert.ToInt32(Item3Price.Text)) + (Convert.ToInt32(Item3Price.Text) * 0.1)).ToString("C2"); 
       } 
      } 

     private void Item4Price_TextChanged(object sender, EventArgs e) 
      { 
       if (Item4Exempt.Checked && Item4Import.Checked) 
       { 
        Item4Output.Text = ((Convert.ToInt32(Item4Price.Text)) + (Convert.ToInt32(Item4Price.Text) * 0.05).ToString("C2")); 
       } 
       else if (Item4Exempt.Checked && !Item4Import.Checked) 
       { 
        Item4Output.Text = (Convert.ToInt32(Item4Price.Text)).ToString("C2"); 
       } 
       else if (!Item4Exempt.Checked && Item4Import.Checked) 
       { 
        Item4Output.Text = ((Convert.ToInt32(Item4Price.Text) + (Convert.ToInt32(Item4Price.Text) * 0.1) + (Convert.ToInt32(Item4Price.Text) * 0.05)).ToString("C2")); 
       } 
       else 
       { 
        Item4Output.Text = ((Convert.ToInt32(Item4Price.Text)) + (Convert.ToInt32(Item4Price.Text) * 0.1)).ToString("C2"); 
       } 
      } 

     private void SalesTax_TextChanged(object sender, EventArgs e) 
      { 
       SalesTax.Text = (((Convert.ToInt32(Item1Output.Text) - Convert.ToInt32(Item1Price.Text)) + ((Convert.ToInt32(Item2Output.Text) - Convert.ToInt32(Item2Price.Text)) + ((Convert.ToInt32(Item3Output.Text) - Convert.ToInt32(Item3Price.Text)) + ((Convert.ToInt32(Item4Output.Text) - Convert.ToInt32(Item4Price.Text)).ToString("C2")); 
      } 

     private void Total_TextChanged(object sender, EventArgs e) 
      { 
       Total.Text = ((Convert.ToInt32(Item1Output)) + (Convert.ToInt32(Item1Output)) + (Convert.ToInt32(Item1Output)) + (Convert.ToInt32(Item1Output)).ToString("C2")); 
      } 

    } 
} 

我遇到的第一個問題是,每當我做的類型分爲Item1Price,輸出到Item1Output,但它不與他人合作,而「銷售稅」和「總」文本框不顯示任何東西。

第二個問題是我無法輸入數字「O.OO」,但我可以做「00」,每當我刪除數字時,它就會崩潰。

任何幫助將非常感激。先謝謝你。

+0

這家公司會爲你提供導師嗎? – ChaosPandion

+8

在四個不同的地方粘貼相同的代碼可能不會給任何潛在的僱主留下深刻的印象。 –

+1

這段代碼非常混亂。您能否請嘗試清理它,指定問題,給我們一個例外信息或類似的信息並重新發布? – dotTutorials

回答

0

首先,它不適合在論壇上發佈編碼任務以獲得工作幫助。如果你不能自己完成編碼任務,我會建議你重新評估你的技能,問問你自己是否準備好了你想要獲得的職位。有了這個說法,我記得在我開始追尋那些職位時的那些日子,當時我可能沒有生意,所以我可以同情這些。現在到你的代碼。

首先,您需要圍繞您的轉換在try catch塊中,應用程序崩潰的原因是因爲0.00不會轉換爲整數,因爲那裏有小數。 00可以工作,但是當你刪除它們時,你的textbox.text值現在不是任何不會轉換的,你的程序會崩潰。所以你需要添加邏輯來處理空字符串值,而不是進行轉換。我建議你在處理貨幣價值時使用十進制數據類型。正如我所建議,我會創建一個方法,您可以傳遞複選框值並返回字符串值來設置您的文本框值。這可以清理你的事件處理程序代碼,因爲它看起來你的計算是相同的。