zoukankan      html  css  js  c++  java
  • 一个求分页的函数

    编写环境:SnippetCompiler

    不需要过多的解释,如有疑问请留言。

    using System;
    using System.Collections.Generic;
     
    public class MyClass
    {
        static int count=1234;
        static int pageSize=10;
        static int startIndex,endIndex;
        public static void RunSnippet()
        {
            GetDataArea(0);
            GetDataArea(1);
            GetDataArea(45);
            GetDataArea(123);
            GetDataArea(124);
            GetDataArea(1999);
        }
        
        private static void GetDataArea(int pageIndex)
        {
            GetDataArea(ref startIndex,ref endIndex,pageIndex,pageSize,count);
            Console.WriteLine("Page{2}\tArea is : \t{0} \t-- \t{1}",startIndex,endIndex,pageIndex);
        }
        
        private static void GetDataArea(ref int startIndex,ref int endIndex,int pageIndex,int pageSize,int count)
        {
            if(pageSize<=0||pageIndex<0||count<=0)
                throw new ArgumentException ("All paramater should bigger than zero!");
            
            try
            {
                startIndex=pageIndex*pageSize;
                endIndex=startIndex+pageSize-1;
            }
            catch
            {
                throw new ArgumentOutOfRangeException("May be the argument too bigger than Design!");
            }
            if(startIndex+1>count)
            {
                GetDataArea(ref startIndex,ref endIndex,pageIndex-1,pageSize,count);
                return;
            }
            if(count<endIndex+1)
                endIndex=count-1;
        }
        
        #region Helper methods
        
        public static void Main()
        {
            try
            {
                RunSnippet();
            }
            catch (Exception e)
            {
                string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
                Console.WriteLine(error);
            }
            finally
            {
                Console.Write("Press any key to continue...");
                Console.ReadKey();
            }
        }
     
        private static void WL(object text, params object[] args)
        {
            Console.WriteLine(text.ToString(), args);    
        }
        
        private static void RL()
        {
            Console.ReadLine();    
        }
        
        private static void Break() 
        {
            System.Diagnostics.Debugger.Break();
        }
     
        #endregion
    }
    作者:KKcat
        
    个人博客:http://jinzhao.me/
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Git最强总结!
    强烈IDEA这些插件,让你的开发速度飞起来!
    MySQL执行计划【explain】详解
    设置php在apache下加载ini配置文件路径,~和curl扩展无法加载的问题
    远程连接mysql数据慢的问题
    在windows下,git webhook使用php拉取代码的学习总结
    centos 添加epel、remi仓库和ELRepo仓库
    windows下mysql数据库表名大小写不敏感
    .gitignore无效,不能过滤某些文件
    编译php时,出错bad interpreter
  • 原文地址:https://www.cnblogs.com/jinzhao/p/1343446.html
Copyright © 2011-2022 走看看