C#嵌套在for循环中的if/else

C#嵌套在for循环中的if/else,c#,if-statement,for-loop,C#,If Statement,For Loop,我在for循环中有一个嵌套的if/else语句,用于确定值是否对数组值有效。它返回的所有值都很好,但是如果if是正确的,它仍然会额外执行else三次。我以为一旦它等于一次,它就会停止,但我想我错过了一些东西 string sectionChoice; int ticketQuantity; double ticketPrice, totalCost; string[] section = { "orchestra", "mezzanine", "balcony", "general" }; do

我在for循环中有一个嵌套的if/else语句,用于确定值是否对数组值有效。它返回的所有值都很好,但是如果if是正确的,它仍然会额外执行else三次。我以为一旦它等于一次,它就会停止,但我想我错过了一些东西

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);
}
字符串段选择;
国际票务质量;
双票价格,总成本;
弦乐[]部分={“管弦乐队”、“夹层”、“阳台”、“将军”};
双[]价格={125.25,62.00,35.75,55.50};
bool isValidSection=false;
sectionChoice=GetSection();
ticketQuantity=GetQuantity();
对于(int x=0;x
当它有效时,它返回如下内容:

int i = section.IndexOf(sectionChoice);
if (i >= 0) {
    ticketPrice = price[i];
} else {
    // Not found!
}
Section section = sections.FirstOrDefault(s => s.Name == sectionChoice);
if (section != null ) ...
你们的价格是32.50英镑。无效条目,x不存在无效条目,x不存在无效条目,x不存在


您要查找的关键字是
break

break
将停止其内部循环的执行。如果您在嵌套循环中,它将只在最内层工作

与此相反的是
continue
<代码>继续
停止该迭代并进入下一个迭代

for(int x=0;x
您要查找的关键字是
break

break
将停止其内部循环的执行。如果您在嵌套循环中,它将只在最内层工作

与此相反的是
continue
<代码>继续
停止该迭代并进入下一个迭代

for(int x=0;x
如果要在满足
条件时停止迭代,则需要使用
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
如果要在满足
条件时停止迭代,则需要使用
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
您真正想做的是确定
部分是否包含特定值,如果包含,则执行一些操作。直接检查一下:

if (section.Contains(sectionChoice))
你也不应该使用。与有两个数组、部分和价格不同,在这两个数组中,每个部分的索引处的对象都“组合”为等于一个值,而您实际建模的似乎是查找特定部分价格的一种方法。这最好使用
字典
建模,该字典可以轻松查找特定键的值。在这里,您的键是部分,值是它的价格

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);
Dictionary ticketsPrices=新字典()
{
{“管弦乐队”,125.25米},
//...
};
bool isValidSection=false;
字符串sectionChoice=GetSection();
int ticketQuantity=GetQuantity();
if(票证价格。集装箱(部分选择))
{
isValidSection=true;
十进制票券价格=票券价格[部分选择];
十进制总成本=CalcTicketCost(票证价格、票证数量);
写入(“\n\n票证的总成本为:{0:c2}”,总成本);
}
其他的
Write(“\n\n无效项,{0}不存在”,sectionChoice);

您真正想做的是确定
部分是否包含特定值,如果包含,则执行一些操作。直接检查一下:

if (section.Contains(sectionChoice))
你也不应该使用。与有两个数组、部分和价格不同,在这两个数组中,每个部分的索引处的对象都“组合”为等于一个值,而您实际建模的似乎是查找特定部分价格的一种方法。这最好使用
字典
建模,该字典可以轻松查找特定键的值。在这里,您的键是部分,值是它的价格

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);
Dictionary ticketsPrices=新字典()
{
{“管弦乐队”,125.25米},
//...
};
bool isValidSection=false;
字符串sectionChoice=GetSection();
int ticketQuantity=GetQuantity();
if(票证价格。集装箱(部分选择))
{
isV
Section section = sections.FirstOrDefault(s => s.Name == sectionChoice);
if (section != null ) ...