循环语句练习(2)

1.数组项目求和

点击查看代码
            int sum = 0;
            int[] s = new int[6] { 2, 3, 4, 7, 9, 6 };
            foreach (int i in s)
            {
                sum += i;
            }
            Console.WriteLine(sum);
2.数组项求最大值和最小值
点击查看代码
            int[] s = new int[6] { 2, 3, 4, 7, 9, 6 };
            int maxs = s[0], mins = s[0];
            foreach (int i in s)
            {
                if (i > maxs) maxs = i;
                if (i < mins) mins = i;
            }
            Console.WriteLine($"数组项中最大值:{maxs},数组项中最小值:{mins}");
3.数组项反转
点击查看代码
            int[] x = new int[6] { 2, 3, 4, 7, 9, 6 };
            Stack<int> stack = new Stack<int>();
            for (int i = 0; i < x.Length; i++)
            {
                stack.Push(x[i]);
            }
            for (int i = 0; i < x.Length; i++)
            {
                x[i] = stack.Pop();
            }
4.数字转换
点击查看代码
            while (true)
            {
                Console.Write("输入1-10的数字:");
                int s = int.Parse(Console.ReadLine());
                switch (s)
                {
                    case 1:
                        Console.WriteLine("壹");
                        break;
                    case 2:
                        Console.WriteLine("贰");
                        break;
                    case 3:
                        Console.WriteLine("叁");
                        break;
                    case 4:
                        Console.WriteLine("肆");
                        break;
                    case 5:
                        Console.WriteLine("伍");
                        break;
                    case 6:
                        Console.WriteLine("陆");
                        break;
                    case 7:
                        Console.WriteLine("柒");
                        break;
                    case 8:
                        Console.WriteLine("捌");
                        break;
                    case 9:
                        Console.WriteLine("玖");
                        break;
                    case 10:
                        Console.WriteLine("拾");
                        break;
                }
            }
posted @ 2023-04-06 21:38  AWT-Stephen  阅读(17)  评论(0编辑  收藏  举报