C# 我能';我不能利用我的第二节课,真的不知道如何解决它
当我运行它时,我所能做的就是写下员工的姓名和销售额,但它不使用第二个类并使用这些信息。有人能帮我吗?我已经为此挣扎了一段时间了C# 我能';我不能利用我的第二节课,真的不知道如何解决它,c#,class,C#,Class,当我运行它时,我所能做的就是写下员工的姓名和销售额,但它不使用第二个类并使用这些信息。有人能帮我吗?我已经为此挣扎了一段时间了 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace consoleapplication9 { public class takehomepay { stati
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();
}
}
}
您需要通过更新对象(Employee)来调用类的构造函数 如果需要任何变量的输出,则需要将其写入控制台:
Console.WriteLine(takehomepay);
而不是
employeeName = Console.ReadLine();
试一试
你可能应该在某个地方创建第二个类的实例。代码中没有提到第二个类的详细信息。你如何创建第二个类的对象,然后访问属性。这是可行的,但它只是给了我totalSales的输入,没有使用它背后的数学,此外,尽管console.read();,它仍会立即关闭;?我在console.read上收到一条警告,检测到哪个代码是无法访问的?我已经做了这么长时间了,现在我完全不知道您的
控制台前面有一个return
语句。请阅读。返回后没有任何内容被执行,因此该代码是不可访问的。另外,Console.Read
语句应该在Main()
的末尾。当我删除返回时,虽然我得到了一个错误(只有赋值、调用、递增、递减、等待和新的对象表达式可以用作语句),那么你在我的Main()结尾是什么意思?对不起,我做这件事已经很久了,现在我正在努力摆脱Console.Read()代码>。相反,把它放在Console.Write(employee)之后代码>谢谢你的工作。很抱歉,但我刚刚意识到我做错了问题,我需要给出takehomepay的输出,这就是为什么我的数学不起作用,因为我使用了错误的变量public override string ToString(){return“Employee:+employeeName+”\n使home pay:+takehomepay;}}}}我将变量更改为takehomepay,但是现在我收到了一个错误takehomepay是一种类型,但被用作变量。为什么它现在这样做?我该如何解决它?谢谢你的帮助,我只是被卡住了
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