我在for循环中有一个嵌套的if / else语句来确定一个值是否对数组值有效。它返回所有值就好了,但是如果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不存在
答案 0 :(得分:4)
您真正要做的是确定section
是否包含特定值,如果有,则执行某些操作。只需直接检查 :
if (section.Contains(sectionChoice))
您也不应该使用parallel arrays。不是有两个数组,而是一个价格,其中每个的索引处的对象“组合”等于一个值,看来你实际建模的是一种查找特定部分价格的方法。这最好使用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);
答案 1 :(得分:1)
您要查找的关键字是break;
break
将停止执行它所在的循环。如果你在嵌套循环中,它只能在最里面工作。
与此相反的是continue
。 Continue
停止迭代并移动到下一个迭代。
Here is an MSDN article explaining it more in depth
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);
}
答案 2 :(得分:1)
获取所选项目的索引,如下所示:
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 ) ...
像这样,更容易向部分添加新属性。例如。您可以添加一些剩余的座位,而无需创建新阵列。
答案 3 :(得分:0)
如果希望在满足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);
}
答案 4 :(得分:0)
您不希望报告每个匹配不匹配的选项无效:
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);