方法一:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("输入一个4位整数:"); int num = scanner.nextInt(); String x= String.valueOf(num); StringBuffer stringBuffer = new StringBuffer(x); String result = stringBuffer.reverse().toString(); System.out.println("反转后整数为:"+result); }
方法二:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("输入一个整数:"); int num = scanner.nextInt(); int resout = 0; while (num != 0){ resout = resout*10+num%10; num/=10; } System.out.println("反转后整数为:"+resout); }
(此方法返回后无法保存首位的0)