java代码如何将一个整数反转(整数倒置)呢?
下文笔者讲述将一个整数反转的方法分享,如下所示
实现思路: 使用while循环 并结合除法即可实现整数反转的效果例:整数反转的示例
public static int reverse(int number) { int result = 0; while (number != 0) { result = result * 10; result = result + number % 10; number = number / 10; } return result; } /** * java265.com 整数反转的示例 * * @throws IOException */ public static void main(String[] args) throws IOException { // 反转一个整数的示例 System.out.println(reverse(9871)); }

版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。