import java.util.Scanner; //取一个整数a从右端开始的4~7位。 public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数:"); String s = ""; Long ls = 0L; while (true) { s = sc.nextLine(); if (s.length() >= 7) { try { ls = Long.parseLong(s); break; } catch (NumberFormatException e) { System.out.println("内容非法,请重新输入一个整数:"); } } else { System.out.println("长度非法,请重新输入一个整数:"); } } StringBuffer sb = new StringBuffer(); sb.append(s); sb.reverse(); String substring = sb.substring(3, 7); System.out.println("取" + ls + "整数a从右端开始的4~7位:" + substring); } }