我重復了一系列 if 陳述句 3 次,我想知道如何解決這個問題。我嘗試制作一種更新價格的方法,但它并沒有讓我呼叫它。有沒有辦法只撰寫一次價格 if 陳述句系列,并在大小 if 陳述句中呼叫它?
我對 C# 很陌生,所以任何幫助將不勝感激。
Console.WriteLine("What size pizza would you like?\nSmall, Medium, or Large? (S, M, L)");
string size = Console.ReadLine();
if(size == "S" || size =="s")
{
decimal price = 5.00M;
Console.WriteLine("How many toppings would you like?\n0, 1, or 2?");
int topping = Convert.ToInt32(Console.ReadLine());
if (topping == 0)
{
price = price 0.00M;
}
else if (topping == 1)
{
price = price 1.00M;
}
else if (topping == 2)
{
price = price 1.50M;
}
price = Math.Round(price (price * 0.10m), 2);
Console.WriteLine("Including 10% tax, you total bill is ${0}", price);
}
else if(size == "M" || size == "m")
{
decimal price = 7.00M;
Console.WriteLine("How many toppings would you like?\n0, 1, or 2?");
int topping = Convert.ToInt32(Console.ReadLine());
if (topping == 0)
{
price = price 0.00M;
}
else if (topping == 1)
{
price = price 1.00M;
}
else if (topping == 2)
{
price = price 1.50M;
}
price = Math.Round(price (price * 0.10m), 2);
Console.WriteLine("Including 10% tax, you total bill is ${0}", price);
}
else if (size == "L" || size == "l")
{
decimal price = 9.00M;
Console.WriteLine("How many toppings would you like?\n0, 1, or 2?");
int topping = Convert.ToInt32(Console.ReadLine());
if (topping == 0)
{
price = price 0.00M;
}
else if (topping == 1)
{
price = price 1.00M;
}
else if (topping == 2)
{
price = price 1.50M;
}
// price = Math.Round(price (price * 0.10m), 2);
price = price (price * 0.10m);
Console.WriteLine("Including 10% tax, you total bill is ${0}", price);
}
uj5u.com熱心網友回復:
據我所見,代碼相同,但價格不同,所以這樣做
void GetOrder(decimal price){
Console.WriteLine("How many toppings would you like?\n0, 1, or 2?");
int topping = Convert.ToInt32(Console.ReadLine());
if (topping == 0)
{
price = price 0.00M;
}
else if (topping == 1)
{
price = price 1.00M;
}
else if (topping == 2)
{
price = price 1.50M;
}
price = Math.Round(price (price * 0.10m), 2);
Console.WriteLine("Including 10% tax, you total bill is ${0}", price);
}
然后做
if(size == "S" || size =="s"){
GetOrder(5.0m);
else if(size == "M" || size == "m")
GetOrder(7.0m)
....
uj5u.com熱心網友回復:
您可以更改方法并在大約 5 行代碼中完成整個操作。這種方法更像是在現實世界中的實作方式,其中價格將存盤在一些可配置的資料存盤中,而不是嵌入到程式的代碼中:
var basePrices = new Dictionary<string, decimal>(){
["S"] = 5,
["M"] = 7,
["L"] = 9
};
string size = Console.ReadLine().ToUpper();
int topping = Convert.ToInt32(Console.ReadLine());
var price = (basePrices[size] (topping > 0 ? topping * 0.5m 0.5m : 0)) * 1.1m
Console.WriteLine("Including 10% tax, you total bill is ${0:0.00}", price);
這里沒有任何錯誤檢查,為簡潔起見,我省略了 WriteLines;練習讓讀者恢復它們并進行錯誤檢查,但傳達的概念是:
您可以使用字典將一個值映射到另一個值,在這種情況下 S => 5 等
字典是區分大小寫的,所以我們 ToUpper 輸入
如果您的厚臉皮用戶將放入 S、M 或 L 以外的其他內容,您可以使用
basePrices.TryGetValue(size, out var basePrice)- 如果未找到大小,則 TryGet Value 回傳 false,因此您可以使用它來給他們一個錯誤訊息而不是價格。同樣對于澆頭,如果他們鍵入 alpha chars Convert.ToInt32 將引發例外,因此您可以查看int.TryParse;與 TryGetValue 的想法相同,它回傳一個表示成功的布林值。你的澆頭是數學的;它們看起來像每個澆頭的成本為 0.5,如果用戶想要它們,再加上 0.5 - 這
test?true value:false value就是topping>0 ? ... : 0計算澆頭價格意味著它們可以以一百萬個澆頭報價。您可以進行上限檢查
您也可以對澆頭價格使用相同的字典概念
var toppingPrices = new Dictionary<int, decimal>(){
[0] = 0,
[1] = 1,
[2] = 1.5
}
- 您可以獲得字串格式化程序來進行四舍五入:格式化
{0:0.00}并在其中拋出任何舊小數
uj5u.com熱心網友回復:
如果在 C# 8 或更高版本上使用帶有模式匹配的switch 運算式:
using static System.Console;
WriteLine(@"What size pizza would you like?
Small, Medium, or Large? (S, M, L)");
var price = ReadLine() switch
{
"S" or "s" => 5.00M,
"M" or "m" => 7.00M,
"L" or "l" => 9.00M,
var invalid => throw new ($"Invalid pizza size: {invalid}")
};
WriteLine(@"How many toppings would you like?
0, 1, or 2?");
price = ReadLine() switch
{
"0" => 0.00M,
"1" => 1.00M,
"2" => 1.50M,
var invalid => throw new ($"Invalid number of toppings: {invalid}")
};
price *= 1.10M;
WriteLine($"Including 10% tax, you total bill is {price:C2}");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/463715.html
上一篇:康威在Matlab中的生活游戲-在嵌套回圈中回傳意外數字的函式
下一篇:python關于值的引數
