java50题----32整数取四位

来源:互联网 发布:室内温度检测软件 编辑:程序博客网 时间:2024/06/05 08:44
/*取一个大于7位的正整数a的从右向左的4~7位。例如:987654321 取7,6,5,4。*/import java.io.*;class Demo{private Demo(){}private static Demo instance = new Demo();public static Demo getInstance(){return instance;}public String getBits(long n){String str = Long.toString(n);char[] arr = str.toCharArray();String newstr = new String(arr, arr.length - 7, 4);return newstr;}}class MainClass {public static void main(String[] args) throws Exception{Demo d = Demo.getInstance();BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));String reg = "[1-9]\\d{6,}";System.out.println("输入一个7位以上的正整数:");for(String str = buf.readLine().trim();true; str = buf.readLine().trim()){if(str.isEmpty() == true)continue;if(str.equals("quit") == true)System.exit(0);if(str.matches(reg) == true){long n = Long.parseLong(str);System.out.println(n+":"+d.getBits(n));}else{System.out.println("输入不合法!!");}}}}/**/

0 0
原创粉丝点击