C# 如何将程序从使用for循环更改为foreach循环
我对如何将for循环更改为foreach循环感到非常困惑。我有一段代码,它类似于我正在开发的一个程序,用于实现foreach循环C# 如何将程序从使用for循环更改为foreach循环,c#,foreach,C#,Foreach,我对如何将for循环更改为foreach循环感到非常困惑。我有一段代码,它类似于我正在开发的一个程序,用于实现foreach循环 { class Program { static void Main(string[] args) { int entered = 0; string ayethatsprettygood; bool dothesalamander = false;
{
class Program
{
static void Main(string[] args)
{
int entered = 0;
string ayethatsprettygood;
bool dothesalamander = false;
string[] zipcode;
zipcode = new string[10] {"12789", "54012", "54481", "54982", "60007", "60103", "60187", "60188", "71244", "90210" }; //This array is the zipcodes
double[] prices;
prices = new double[10] { 2.40, 3.00, 3.50, 4.00, 4.50, 5.00, 5.25, 5.75, 6.10, 10.003 }; //This array is the prices
Console.Write("Enter a Zipcode:");
ayethatsprettygood = Console.ReadLine();
for (int i = 0; i < zipcode.Length; i++) {
if (ayethatsprettygood == zipcode[i])
{
dothesalamander = true;
entered = i;
break;
}
}
if (dothesalamander == true)
{
Console.WriteLine("We deliver to this zipcode {0} the price of delivery is {1}", zipcode[entered], prices[entered].ToString("C2"));
}
else
Console.WriteLine("We don't deliver to this zipcode {0}",ayethatsprettygood);
{
class Program
{
static void Main(string[] args)
{
int entered = 0;
string ayethatsprettygood;
bool dothesalamander = false;
char[] pizzasize;
pizzasize = new char[4] { 'S', 'M', 'L', 'X' }; //This array is the zipcodes
double[] pizzaprices;
pizzaprices = new double[4] { 6.99, 8.99, 12.50, 15.00 }; //This array is the prices
Console.Write("Enter a Pizza Size:");
ayethatsprettygood = Console.ReadLine();
foreach(char pizzaprice in pizzaprices )
{
if (ayethatsprettygood == pizzaprice)
{
dothesalamander = true;
entered = pizzaprice;
break;
}
}
if (dothesalamander == true)
{
Console.WriteLine("The size of the pizza is {0} the price is {1}", pizzasize[entered], pizzaprices[entered].ToString("C2"));
}
else
Console.WriteLine("Sorry you entered an invalid size {0}", ayethatsprettygood);
}
}
}
{
班级计划
{
静态void Main(字符串[]参数)
{
输入的int=0;
串起一条美好的线;
bool dothesalamander=false;
字符串[]zipcode;
zipcode=新字符串[10]{“12789”、“54012”、“54481”、“54982”、“60007”、“60103”、“60187”、“60188”、“71244”、“90210”};//此数组是zipcode
双[]价格;
prices=new double[10]{2.40,3.00,3.50,4.00,4.50,5.00,5.25,5.75,6.10,10.003};//这个数组就是价格
编写(“输入Zipcode:”);
ayethatsprettygood=Console.ReadLine();
for(int i=0;i
如果有人能帮我完成这个项目,那将非常有帮助!
谢谢!使用普通的for循环,您可以自己管理索引,如果要遍历数组,则必须使用索引查找项目
var a = new string[] {"This","Is","An","Array"};
for (var i = a.GetLowerBound(0); i <= a.GetUpperBound(0); i = i + 1)
{
var s = a[i];
Console.WriteLine(s);
}
大多数数组、列表、词典和其他类型的集合都实现了IEnumerable
,因此您可以使用此技术浏览大多数列表。如果IEnumerable
不可用,则无法使用foreach
foreach
构造需要更少的键入,迭代可以并行运行(使用parallel.foreach
),因此它有一些优点。主要缺点是,再加上您无法访问包含索引的变量。在某些情况下,这可能是一个问题,例如在两个数组之间复制数据,其中顺序可能很重要
此外,在使用枚举器时,删除和添加项如果不是直接删除或添加,也会引起混乱,这是非常不鼓励的。使用普通的for循环,您可以自己管理索引,如果您在数组中进行迭代,则必须使用索引查找项
var a = new string[] {"This","Is","An","Array"};
for (var i = a.GetLowerBound(0); i <= a.GetUpperBound(0); i = i + 1)
{
var s = a[i];
Console.WriteLine(s);
}
大多数数组、列表、词典和其他类型的集合都实现了IEnumerable
,因此您可以使用此技术浏览大多数列表。如果IEnumerable
不可用,则无法使用foreach
foreach
构造需要更少的键入,迭代可以并行运行(使用parallel.foreach
),因此它有一些优点。主要缺点是,再加上您无法访问包含索引的变量。在某些情况下,这可能是一个问题,例如在两个数组之间复制数据,其中顺序可能很重要
此外,在使用枚举器时,删除和添加项如果不是直接的,也会因为它可能会带来混乱而受到强烈劝阻。
foreach
在使用并行数组时不是一个很好的选择(提示,并行数组在“真实”代码中并不经常使用)
原因是,正如您所发现的,您不知道给定项的索引是什么(foreach
不保证线性遍历,尽管在实践中经常如此)。以下是解决此问题的三种方法:
假设线性遍历 您可以在循环外部设置一个计数器,并在每次迭代中递增。假设
foreach
从索引0开始,递增1,则您仍然拥有索引:
int iterationIndex = 0;
foreach (item in collection)
{
iterationIndex++;
}
当然,如果假设是错误的(在实践中,遍历通常是线性的,并且总是在数组和列表上),那么代码就会中断
搜索数组 您知道要查找的项目;可以使用数组/列表方法获取其索引:
int index = collection.IndexOf(item);
var parallelItem = parallelCollection[index];
如果集合
有多个项
实例(实际上是一个重复的键),则会出现此问题
使用类(正确答案!) 如果您放弃并行数组并使用对象,所有这些都是没有意义的。假设我有一个pizza类:
public class Pizza
{
public double Size {get; set}
public double Price {get; set;}
}
现在数据关联由框架处理,我可以写:
Pizza requestedPizza;
foreach (Pizza pizza in allPizzas)
{
if (pizza.Price == enteredPrice)
{
requestedPizza = pizza;
break;
}
}
Console.WriteLine($"Found pizza size {requestedPizza.Size} that matches price {requestedPizza.Price}");
为了将来使用,整个foreach
完全没有必要,LINQ将为我们编写:
Pizza requestedPizza = allPizzas.FirstOrDefault(p => p.Price == requestedPrice);
使用并行数组时,
foreach
不是一个很好的选择(提示,并行数组不是