在 Java 中反轉整數

Sheeraz Gul 2023年1月30日 2022年4月27日
  1. 在 Java 中使用遞迴來反轉整數
  2. 在 Java 中使用 while 迴圈反轉整數
  3. 在 Java 中使用 for 迴圈反轉整數
在 Java 中反轉整數

本教程將演示如何在不使用陣列的情況下在 Java 中反轉整數。

在不使用陣列的情況下反轉整數時,我們需要遵循三個步驟。

  • 使用模 (%) 運算子查詢整數的餘數。
  • 將變數 reverse 乘以 10,然後將其與餘數相加。
  • 將數字除以 10。

在 Java 中使用遞迴來反轉整數

遞迴是使用模數和除法運算子反轉整數的最簡單方法。

例子:

import java.util.Scanner;
public class Reverse_Recursion{
    //Create a method to reverse a number using recursion
    public static void Recursion_reverse(int input_number){
        if (input_number < 10){
        //If the number is less then 10 then no need to reverse
            System.out.println(input_number);
            return;
        }
        else{
            System.out.print(input_number % 10);
            Recursion_reverse(input_number/10);
        }
    }
    public static void main(String args[]){
        System.out.print("Enter the Integer you want to Reverse: ");
        Scanner input_num = new Scanner(System.in);
        int input_number = input_num.nextInt();
        System.out.print("The reverse of the given number using recursion is: ");
        Recursion_reverse(input_number);
    }
}

輸出:

Enter the Integer you want to Reverse: 1234567890
The reverse of the given number using recursion is: 0987654321

在 Java 中使用 while 迴圈反轉整數

要使用 while 迴圈反轉整數,我們必須遵循提到的所有三個步驟。

例子:

import java.util.Scanner;
public class Reverse_While{
    public static void main(String args[]){
        System.out.print("Enter the Integer you want to Reverse: ");
        Scanner input_num = new Scanner(System.in);
        int input_number = input_num.nextInt();
        int reverse_number = 0;
        while(input_number != 0){
            int remainder_number = input_number % 10;
            reverse_number = reverse_number * 10 + remainder_number;
            input_number = input_number/10;
        }
        System.out.println("The reverse of the given number using while loop is: " + reverse_number);
    }
}

輸出:

Enter the Integer you want to Reverse: 123456789
The reverse of the given number using while loop is: 987654321

在 Java 中使用 for 迴圈反轉整數

for 迴圈也類似於 while 迴圈反轉整數。

這次讓我們嘗試反轉一個負整數。

例子:

import java.util.Scanner;
public class Reverse_For{
    public static void main(String args[])  {
        System.out.print("Enter the Integer you want to Reverse: ");
        Scanner input_num = new Scanner(System.in);
        int input_number = input_num.nextInt();
        int reverse_number = 0;
        for( ;input_number != 0; input_number=input_number/10){
            int remainder_number = input_number % 10;
            reverse_number = reverse_number * 10 + remainder_number;
        }
        System.out.println("The reverse of the given number using while loop is: " + reverse_number);
    }
}
Enter the Integer you want to Reverse: -123456789
The reverse of the given number using while loop is: -987654321
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

相關文章 - Java Integer