TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- 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===============
- }
- }
- 运行:
- [img]http://img.javaxxz.com/2014/11/12/235720312.gif[/img]
复制代码
源码下载:http://file.javaxxz.com/2014/11/12/235720484.zip |
|