2014-09-26 27 views
-6

當我運行它時,我所能做的就是編寫員工姓名和銷售額,但它不使用第二類,並使用它信息。有人能幫助我嗎?我一直在爲此苦苦掙扎!我不能使用我的第二課,真的不確定如何解決它

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace consoleapplication9 
{ 
public class takehomepay 
{ 
    static void Main(String[] args) 
    { 
     const decimal commission = 0.7M; // Commision rate 
     const decimal federaltax = 0.18M; // federal tax rate 
     const decimal retirement = 0.10M; // retirement rate 
     const decimal socialsecurity = 0.06M; // social security rate 

     string employeeName; 
     decimal commcost = 0; // commision cost 
     decimal fedtaxcost = 0; // federal tax cost 
     decimal retirecost = 0; // retirement cost 
     decimal socseccost = 0; // social security cost 
     decimal totalwithholdingcost = 0; // total withholding 
     decimal takehomepay = 0; // amount taken home 
     decimal totalSales = 0; 

     Console.Write("\nEnter employees name: "); 
     employeeName = Console.ReadLine(); 

     Console.Write("Enter the total sales amount for the week:"); 
     totalSales = Convert.ToDecimal(Console.ReadLine()); 

     //Calculations 
     commcost = commission * totalSales; 
     fedtaxcost = federaltax * commcost; 
     retirecost = retirement * commcost; 
     socseccost = socialsecurity * commcost; 
     totalwithholdingcost = federaltax + retirement + socialsecurity; 
     takehomepay = commcost - totalwithholdingcost; 
    } 
} 

public class Employee 
{ 
    private string employeeName; 
    private decimal totalSales; 
    public Employee() 
    { 
    } 
    public Employee (string Name) 
    { 
     employeeName = Name; 
    } 
    public Employee(string Name, decimal Sales) 
    { 
     employeeName = Name; 
     totalSales = Sales; 
    } 
    public string EmployeeName 
    { 
     get 
     { 
      return employeeName; 
     } 
     set 
     { 
      employeeName = value; 
     } 

    } 

    public decimal TotalSales 
    { 

     get 
     { 
      return totalSales; 
     } 

     set 
     { 
      totalSales = value; 
     } 
    } 
    public override string ToString() 
    { 
     return "Employee: " + employeeName + 
      "\nTotal Sales: " + totalSales; 
     Console.Read(); 
    } 
} 
} 
+6

你或許應該某處創建第二類的實例.. – 2014-09-26 03:12:33

+0

二等細節不是你的代碼,你可以怎麼過創建第二類的對象,然後訪問屬性中提到。 – Prabhakantha 2014-09-26 03:19:14

回答

2

您需要通過newing了一個對象(僱員)

Console.Write("\nEnter employees name: "); 
    employeeName = Console.ReadLine(); 

    Console.Write("Enter the total sales amount for the week:"); 
    totalSales = Convert.ToDecimal(Console.ReadLine()); 

    //Create an instance of your second class... 
    var employee = new Employee(employeeName,totalSales); 
    Console.Write(employee); 

如果您從您的任何變量,你需要將它們寫入控制檯需要輸出調用類的構造函數:

Console.WriteLine(takehomepay); 
+0

這有效,但它只是給我輸入totalSales,不使用它後面的數學,儘管console.read();它也立即關閉;? 雖然在console.read中檢測到無法訪問的代碼,但我收到警告了?我剛剛這麼做了很長時間我只是不知道 – 2014-09-26 03:26:47

+0

你在'Console.Read'之前有'return'語句。返回執行後沒有任何內容,因此該代碼*不可訪問*。另外,'Console.Read'語句最後應該在'Main()'中。 – 2014-09-26 04:14:31

+0

當我刪除返回,雖然我得到一個錯誤 錯誤(只能分配,調用,增量,減量,等待,和新的對象表達式可以作爲一個聲明) 也是什麼意思在我的主要()在結束? 對不起,我剛剛這麼做了這麼久,現在我只是在掙扎 – 2014-09-26 04:22:48

0

而不是

employeeName = Console.ReadLine(); 

嘗試

Employee employee = new Employee();// this creates an instance of the employee class 
employee.EmloyeeName = Console.ReadLine();// this is how you use that instance 
相關問題