如何为每个循环转换我的循环?

编程入门 行业动态 更新时间:2024-10-28 06:26:54
本文介绍了如何为每个循环转换我的循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用for循环制作了一个菱形图案程序。我想用foreach循环来做。任何人都可以请帮助我如何将我的for循环转换为foreach循环。 这是代码:

I have made a diamond pattern program using for loop. I want to do it using foreach loop. Any one can please help me out how do i convert my for loops to foreach loop. Here is code:

Console.WriteLine("Program for displaying pattern of *."); Console.Write("Enter the maximum number of *: "); int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("\nHere is the Diamond of Stars\n"); for (int i = 0; i <= (n - 1); i = i + 2) { for (int j = 0; j <= i; j++) Console.Write("*"); Console.WriteLine(); } for (int i = n - 1; i >= 1; i = i - 2) { for (int j = 0; j < (i - 1); j++) Console.Write("*"); Console.WriteLine(); }

推荐答案

在这些情况下,尝试没有任何意义:你没有使用对象的集合,因此没有foreach循环的基础 - 这些仅在您通过实现IEnumerable或IEnumerable< T>的对象集合进行迭代时起作用。接口。 Int32 不这样做,所以你不能使用 foreach 替换 for 循环。 In those cases, it makes no sense to try: you aren't using a collection of objects, so there is no base for a foreach loop - these only work when you are iterating though a collection of objects that implements the IEnumerable or IEnumerable<T> interface. Int32 doesn't do that, so you can't use a foreach to replace your for loops.

更多推荐

如何为每个循环转换我的循环?

本文发布于:2023-11-29 19:11:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!