• C# 判断一个整数是否是素数!使用bool IsPrim(int n)实现!


    static void Main(string[] args)
            {

                string input = "";
                bool result;
                int number = 0;
                do
                {
                    Console.WriteLine("请输入一个正整数:");
                    input = Console.ReadLine();
                    if (input=="q")
                    {
                        Console.WriteLine("程序结束!");
                        break;
                    }
                    try
                    {
                        number = Convert.ToInt32(input);
                        result = IsPrim(number);
                        if (result==true)
                        {
                            Console.WriteLine("{0}是素数!", number);
                        }
                        else
                        {
                            Console.WriteLine("{0}不是素数!",number);
                        }
                    }
                    catch
                    {
                        Console.WriteLine("输入数据有误,请重新输入!");
                        continue;
                    }
                } while (true);
                Console.ReadKey();
            }
            public static bool IsPrim(int number)
            {
                for (int i = 2; i < number; i++)
                {
                    if (number%2==0)
                    {
                        return false;
                    }
                }
                return true;
            }
            public static int ReadInt()
            {
                int number = 0;
                do
                {
                    try
                    {
                        number = Convert.ToInt32(Console.ReadLine());
                        if (number<=0)
                        {
                            Console.WriteLine("输入有误!请输入一个正数!");
                            continue;
                        }
                    }
                    catch
                    {
                        Console.WriteLine("输入有误,请重新输入!");
                    }
                } while (true);
            }

  • 相关阅读:
    Web打印控件
    excel错误:外部表不是预期的格式 错误
    C#用ado.net访问EXCEL的常见问题及解决方法
    通过反射的方式获取类型中的所有属性
    在64位Windows7上安装64位Oracle11g
    2020&2021的计划
    jQuery_day1
    springboot+mybatis+MySQL(入门级-半小时搞定系列)
    springboot_web开发
    springboot日志
  • 原文地址:https://www.cnblogs.com/bby2014210552/p/9754714.html
Copyright © 2020-2023  润新知