[英]C# nested if/else in a for loop
我在for循環中有一個嵌套的if / else語句,以確定數組值對aa值是否有效。 它返回的所有值都很好,但是如果IF是正確的,它仍然會執行其他三遍。 我以為一旦等於一次,它將停止,但是我想我在這里錯過了一些東西。
string sectionChoice;
int ticketQuantity;
double ticketPrice, totalCost;
string[] section = { "orchestra", "mezzanine", "balcony", "general" };
double[] price = { 125.25, 62.00, 35.75, 55.50 };
bool isValidSection = false;
sectionChoice = GetSection();
ticketQuantity = GetQuantity();
for (int x = 0; x < section.Length; ++x)
{
if (sectionChoice == section[x])
{
isValidSection = true;
ticketPrice = price[x];
totalCost = CalcTicketCost(ticketPrice, ticketQuantity);
Console.Write("\n\nTotal cost for the tickets are: {0:c2}", totalCost);
}
else
Console.Write("\n\nInvalid entry, {0} does not exist", sectionChoice);
}
有效時,它將返回如下內容:
您的價格是32.50。 無效的條目,x不存在無效的條目,x不存在無效的條目,x不存在
您真正想做的是確定section
包含特定值,如果存在則執行某些操作。 只要檢查直接 :
if (section.Contains(sectionChoice))
您也不應該使用並行數組 。 與其有兩個數組,沒有一個價格,每個對象的索引處的對象“組合”等於一個值,而是您實際上建模的是一種查找特定價格的手段。 最好用一個Dictionary
建模,該Dictionary
可以輕松查找特定鍵的值。 在這里,您的關鍵是該部分,價值是其價格。
Dictionary<string, decimal> ticketsPrices = new Dictionary<string, decimal>()
{
{"orchestra", 125.25m},
//...
};
bool isValidSection = false;
string sectionChoice = GetSection();
int ticketQuantity = GetQuantity();
if (ticketsPrices.ContainsKey(sectionChoice))
{
isValidSection = true;
decimal ticketPrice = ticketsPrices[sectionChoice];
decimal totalCost = CalcTicketCost(ticketPrice, ticketQuantity);
Console.Write("\n\nTotal cost for the tickets are: {0:c2}", totalCost);
}
else
Console.Write("\n\nInvalid entry, {0} does not exsist", sectionChoice);
您要尋找的關鍵字是break;
break
將停止其內部循環的執行。 如果您在嵌套循環中,則只能在最內層使用。
相反的是continue
。 Continue
停止該迭代,然后移至下一個。
for (int x = 0; x < section.Length; ++x)
{
if (sectionChoice == section[x])
{
isValidSection = true;
ticketPrice = price[x];
totalCost = CalcTicketCost(ticketPrice, ticketQuantity);
Console.Write("\n\nTotal cost for the tickets are: {0:c2}", totalCost);
break;
}
else
Console.Write("\n\nInvalid entry, {0} does not exsist", sectionChoice);
}
獲取所選項目的索引,如下所示:
int i = section.IndexOf(sectionChoice);
if (i >= 0) {
ticketPrice = price[i];
} else {
// Not found!
}
但是,使用項目類,您的代碼將變得更加靈活
public class Section
{
public string Name { get; set; }
public decimal Price { get; set; }
}
借助此類,您可以創建像這樣的部分列表
List<Item> sections = new List<item> {
new Section { Name = "orchestra", Price = 125.25 },
new Section { Name = "mezzanine", Price = 62.00 },
...
};
現在您可以找到這樣的部分:
Section section = sections.FirstOrDefault(s => s.Name == sectionChoice);
if (section != null ) ...
這樣,將新屬性添加到各節中就更加容易。 例如,您可以在其中添加許多剩余席位,而不必創建新的數組。
如果要在滿足if
條件時停止迭代, if
需要使用break
語句退出循環:
for (int x = 0; x < section.Length; ++x)
{
if (sectionChoice == section[x])
{
isValidSection = true;
ticketPrice = price[x];
totalCost = CalcTicketCost(ticketPrice, ticketQuantity);
Console.Write("\n\nTotal cost for the tickets are: {0:c2}", totalCost);
break; // THIS IS THE IMPORTANT CHANGE
}
}
if (!isValidSection) // TEST MOVED OUTSIDE OF LOOP
{
Console.Write("\n\nInvalid entry, {0} does not exsist", sectionChoice);
}
您不想報告每個不匹配的選擇都是無效的選擇:
for (int x = 0; x < section.Length; ++x)
{
if (sectionChoice == section[x])
{
isValidSection = true;
ticketPrice = price[x];
totalCost = CalcTicketCost(ticketPrice, ticketQuantity);
Console.Write("\n\nTotal cost for the tickets are: {0:c2}", totalCost);
break;
}
}
if (!isValidSection)
Console.Write("\n\nInvalid entry, {0} does not exist", sectionChoice);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.