疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

java命令行分页代码


 

    我们用java命令实现行分页的代码:

  import java.util.Scanner;

  public class Test {

  public static void main(String[] args) {

  while(true){

  System.out.print("页码:");

  Scanner scan = new Scanner(System.in);

  int ind=1;

  try {

  ind=Integer.parseInt(scan.next());

  } catch (Exception e) {System.exit(0);}

  fenye(ind,1203,20,4);

  }

  }

  /**

  * 分页实例

  * @param cp//当前页[>1]

  * @param ac//总条数[>1]

  * @param pn//一页条数[>1]

  * @param num//按钮数,偶数[>2]

  */

  public static void fenye(int cp,int ac,int pn,int num){

  if(num< 2) num=8;

  int no=num%2==0?num/2:num/2+1;

  int ap=ac%pn==0?ac/pn:ac/pn+1;

  //========begin===============

  StringBuffer rt=new StringBuffer();

  if(cp>no+1) rt.append("[ 1 ] ");

  if(cp>no+2) rt.append(" [ ... ] ");

  int x=-no;

  if(ap-cp-no< 0){x+=ap-cp-no;}

  for (int i = -no; i <= no; i++) {

  int a= cp+x;x++;

  if(a< 1){ i--; continue;}

  if(a>ap) continue;

  if(x==1) rt.append("【"+a+"】");

  else rt.append(" [ "+a+" ] ");

  }

  if(cp< (ap-no)) rt.append(" [ ... ] ");

  if(cp< (ap-no+1)) rt.append(" [ "+ap+" ] ");

  System.out.println(rt);

  //========end===============

  }

  }

  运行: