C # Chapter II practice

Input there are several ten even-number calculating

Main entry procedure is executed
little distinction between the following two procedures, comparison Note:

Case 1:
Check dozens of input data is ten in the even

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;
            }
        }
    }
}

This program does not determine whether the data input is not illegal data
Here Insert Picture Description


Scenario 2:
In the process of the input data can be verified whether all the input number string, and if the mixing will re-enter the symbol. For example: "123asdf", if the input data that will let you re-enter illegally.

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;
            }
        }
    }
}


Observe when the input data is illegal, and you will have input.
Here Insert Picture Description

Published 46 original articles · won praise 21 · views 9955

Guess you like

Origin blog.csdn.net/qq_41503174/article/details/104507995