C#第二章实践

输入十个数计算里面有几个偶数

Main方法是程序执行的入口
下面的两个程序之间有点小区别,注意比较:

情况一:
输入十个数据会检查十个数中的偶数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class MyClass
    {
        public static int countEvenNum(int[] arr)
        {
            try//可能出现异常
            {
                int count = 0;
                for (int i = 0; i < arr.Length; i++)
                {
                    if ((arr[i]!=0)&&(arr[i]%2==0))
                    {
                        count++;
                    }
                }
                return count;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string s;
                int i = 0;
                int[] a = new int[10];
                //循环输入十个数字
                while (i<a.Length)
                {
                    Console.Write("请输入第{0}个整数:",i);
                    s = Console.ReadLine();
                }
                    //int.tryparse()这个方法当输入有效数据时,会将s转换成int型,然后存入a数组位置中;如无效,则a数组相应位置值不变
                    //无效就是说不能够 正确转换 ,比如说:"12asd"这个字符串不能够转成int数据
                    int.TryParse(s, out a[i]);
                    i++;
                }
                //将输入的数字输出
                foreach (int item in a)
                {
                    Console.WriteLine(item);
                }
                //调用类中的方法来统计输入数据中的偶数的个数
                //如果类中的方法是静态的(static),那么可以用类名来调用
                int k = MyClass.countEvenNum(a);
                //利用任何对象都一个tostring方法
                Console.WriteLine("偶数的个数是:" + k.ToString());
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

这个程序不会判断输入的数据是不是非法数据
在这里插入图片描述


情况二:
在输入数据的过程中能够验证输入的字符串是否全是 数,如果由符号混入会让及重新输入。例如:“123asdf”,如果输入这种非法数据会让你重新输入。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class MyClass
    {
        public static bool IsIntNum(string input)
        {
            //用正则表达式来检查输入的字符串是不是由纯数字构成
            Regex reg = new Regex("^\\d+$");
            Match m = reg.Match(input);
            return m.Success;
        }
        public static int countEvenNum(int[] arr)
        {
            try//可能出现异常
            {
                int count = 0;
                for (int i = 0; i < arr.Length; i++)
                {
                    if ((arr[i]!=0)&&(arr[i]%2==0))
                    {
                        count++;
                    }
                }
                return count;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string s;
                int i = 0;
                int[] a = new int[10];
                //循环输入十个数字
                while (i<a.Length)
                {
                    Console.Write("请输入第{0}个整数:",i);
                    s = Console.ReadLine();
                    if (!MyClass.IsIntNum(s))
                    {
                        Console.Write("请输入正整数!");
                        // continue的作用是 使本次循环终止,进行下一次循环
                        //注意i的值没有改变
                        continue;
                    }
                    else
                    {
                        a[i] =int.Parse(s);
                        i++;
                    }
                    ////int.tryparse()这个方法当输入有效数据时,会将s转换成int型,然后存入a数组位置中;如无效,则a数组相应位置值不变
                    ////无效就是说不能够 正确转换 ,比如说:"12asd"这个字符串不能够转成int数据
                    //int.TryParse(s, out a[i]);
                    //i++;
                }
                //将输入的数字输出
                foreach (int item in a)
                {
                    Console.WriteLine(item);
                }
                //调用类中的方法来统计输入数据中的偶数的个数
                //如果类中的方法是静态的(static),那么可以用类名来调用
                int k = MyClass.countEvenNum(a);
                //利用任何对象都一个tostring方法
                Console.WriteLine("偶数的个数是:" + k.ToString());
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}


注意观察当输入非法数据时,会让你一直输入。
在这里插入图片描述

发布了46 篇原创文章 · 获赞 21 · 访问量 9955

猜你喜欢

转载自blog.csdn.net/qq_41503174/article/details/104507995