我正在尝试创建一个程序,允许用户输入订单的成本,并根据他们的输入计算销售税和运费。我希望它能够为用户提供确定总计以及总计所涉及的所有不同成本。我是C#的新手,并且不明白为什么我的主要课程不能识别我的第二课中的变量名,以及我如何让他们进行互动。我非常感谢任何意见。提前致谢。到目前为止,这是我的代码:
class SportGoods
{
decimal SubTotal = 0;
decimal SalesTax = 0;
decimal ShippingAmount = 0;
decimal GrandTotal = 0;
string State = ("");
public decimal GrandTotal { get; set; }
public decimal SubTotal { get; set; }
public string State { get; set; }
public decimal SalesTax { get; set; }
public decimal ShippingAmount { get; set; }
public decimal AZTax = 1.05m;
public decimal CATax = 1.10m;
public decimal LessThan50 = 12.99m;
public decimal FiftyTo100 = 4.99m;
public SportGoods(decimal subTotal, decimal salesTax, decimal
shippingAmount, decimal grandTotal)
{
SubTotal = subTotal;
SalesTax = salesTax;
ShippingAmount = shippingAmount;
GrandTotal = grandTotal;
}
public decimal subTotal
{
set
{
SubTotal = value;
}
}
public void CalculateSalesTax()
{
if (State == "AZ")
{
SalesTax = (SubTotal * AZTax) - SubTotal;
}
else if (State == "CA")
{
SalesTax = (SubTotal * CATax) - SubTotal;
}
else
{
SalesTax = 0;
}
}
public void CalculateShipCost()
{
if (SubTotal >= 0 && SubTotal < 50 )
{
ShippingAmount = LessThan50;
}
else if (SubTotal >= 50 && SubTotal < 100)
{
ShippingAmount = FiftyTo100;
}
else if (SubTotal >= 100)
{
ShippingAmount = 0;
}
else
{
Console.WriteLine("Your input was invalid");
}
}
public void CalculateGrandTotal()
{
GrandTotal = SubTotal + SalesTax + ShippingAmount;
}
}
}
我的主要:
class Program
{
static void Main(string[] args)
{
SportGoods info = new SportGoods((decimal)SubTotal,
(decimal)SalesTax, (decimal)ShippingAmount, (decimal)GrandTotal);
//THE ABOVE CAUSES AN ERROR
Console.WriteLine("Hello, and welcome to Dan's Sporting
Goods. This program will allow you to calculate the tax and
shipping charges for any order that you would like to make.
Before using the program, please determine what it is you would
like to buy from our website, and how much your order will cost
in total, before shipping and tax (The subtotal).");
Console.WriteLine("");
Console.WriteLine("How much is the total cost of your order?");
SubTotal = Console.ReadLine();//THIS CAUSES AN ERROR
Console.WriteLine("Please enter your state's initials (For
California, use 'CA', for Arizona, use AZ, etc.):")
State = Console.ReadLine(); //THIS CAUSES AN ERROR
}
}
}
答案 0 :(得分:0)
我在代码中将我的解释作为注释。请阅读。希望能帮助到你。祝你好运!
课堂答案:
class SportGoods
{
public decimal GrandTotal { get; set; }
public decimal SubTotal { get; set; }
public string State { get; set; }
public decimal SalesTax { get; set; }
public decimal ShippingAmount { get; set; }
public decimal AZTax = 1.05m;
public decimal CATax = 1.10m;
public decimal LessThan50 = 12.99m;
public decimal FiftyTo100 = 4.99m;
public SportGoods(decimal subTotal, decimal salesTax, decimal
shippingAmount, decimal grandTotal)
{
SubTotal = subTotal;
SalesTax = salesTax;
ShippingAmount = shippingAmount;
GrandTotal = grandTotal;
}
/* Remove this. It is redundant as SubTotal can do the same. */
//public decimal subTotal
//{
// set
// {
// SubTotal = value;
// }
//}
/* Make this private, we don't need to expose this.
* We just need to expose something simple and straight forward. */
private void CalculateSalesTax()
{
if (State == "AZ")
{
SalesTax = (SubTotal * AZTax) - SubTotal;
}
else if (State == "CA")
{
SalesTax = (SubTotal * CATax) - SubTotal;
}
else
{
SalesTax = 0;
}
}
/* Make this private, we don't need to expose this.
* We just need to expose something simple and straight forward. */
private void CalculateShipCost()
{
if (SubTotal >= 0 && SubTotal < 50 )
{
ShippingAmount = LessThan50;
}
else if (SubTotal >= 50 && SubTotal < 100)
{
ShippingAmount = FiftyTo100;
}
else if (SubTotal >= 100)
{
ShippingAmount = 0;
}
/* Do checking elsewhere. Removed input invalid. */
}
/* Make this private, we don't need to expose this.
* We just need to expose something simple and straight forward. */
private void CalculateGrandTotal()
{
GrandTotal = SubTotal + SalesTax + ShippingAmount;
}
/* Expose this. Just simple method name called "PerformCalculation". Clear cut. */
public void PerformCalculation()
{
CalculateSalesTax();
CalculateShipCost();
CalculateGrandTotal();
}
}
回答主要问题:
static void Main(string[] args)
{
/* Initialize everyting to 0 first. */
SportGoods info = new SportGoods(0,0,0,0);
/* Use this to check whether user input is valid. I put pass = false first
* in order for the while loop logic below to go through at least once. */
bool pass = false;
Console.WriteLine(@"Hello, and welcome to Dan's Sporting
Goods. This program will allow you to calculate the tax and
shipping charges for any order that you would like to make.
Before using the program, please determine what it is you would
like to buy from our website, and how much your order will cost
in total, before shipping and tax (The subtotal).");
Console.WriteLine("");
Console.WriteLine(@"How much is the total cost of your order?");
/* Make sure user enter numeric. As long as the input does not pass
* the checking point (pass==false / !pass), show error and continue
* to loop. */
while (!pass)
{
/* Use try catch to catch error. */
try
{
/* Convert will throw error if the input is non-numeric. */
info.SubTotal = Convert.ToDecimal(Console.ReadLine());
/* If no error is thrown, checking is okay, so set pass = true. */
pass = true;
}
catch
{
Console.WriteLine("Invalid Input. Please enter numeric value!" + "\n\n");
Console.WriteLine(@"How much is the total cost of your order?");
}
}
if (pass)
{
/* For simplicity's sake, I don't perform checking for states. */
Console.WriteLine(@"Please enter your state's initials
(For California, use 'CA', for Arizona, use AZ, etc.):");
info.State = Console.ReadLine();
/* This is the new method I added in. Refer to SportGoods class. */
info.PerformCalculation();
Console.Write("Your Order cost is: " + info.SubTotal + "\n\n");
Console.Write("Your Sales Tax is: " + info.SalesTax + "\n\n");
Console.Write("Your Shipping Cost is: " + info.ShippingAmount + "\n\n");
Console.Write("Your Grand Total is: " + info.GrandTotal + "\n\n");
Console.Write("Press ENTER to end.");
Console.Read();
}
}