如何在Java中檢查一個數是否為迴文數?


迴文數可以定義為其數字不遵循遞增或遞減順序的正數。

這意味著數字是無序的。

1到100之間沒有迴文數。

舉幾個例子:

例1

輸入數字為101

讓我們使用迴文數的邏輯來檢查它:

1 > 0 and 0 < 1

因此,101是一個迴文數。

例2

輸入數字為905

讓我們使用迴文數的邏輯來檢查它:

9 > 0 and 0 < 5

因此,905是一個迴文數。

例3

輸入數字為245

讓我們使用迴文數的邏輯來檢查它:

2 < 4 but 4 < 5 means all digits are in decreasing order.

因此,245不是一個迴文數。

其他一些迴文數的例子包括102、564、897等等。

語法

我們可以使用內建的toString()方法將整數值轉換為字串值。

以下是它的語法:

String str = Integer.toString(inputNumber)

我們可以使用charAt()方法從字串中選擇任何索引處的字元。以下是它的語法:

char ch = str.charAt(0);

演算法

  • 步驟1 - 透過初始化或使用者輸入獲取一個整數。

  • 步驟2 - 將標誌設定為true,並檢查數字是否按升序或降序排列。

  • 步驟3 - 如果數字遵循升序或降序,則將標誌設定為false。

  • 步驟4 - 列印結果。

多種方法

我們提供了不同的方法來解決這個問題。

  • 使用靜態輸入值

  • 使用使用者自定義方法和charAt()方法

讓我們一一檢視程式及其輸出。

方法1:使用靜態輸入值

在這種方法中,整數值將在程式中初始化,然後使用演算法來檢查該數是否為迴文數。

示例

import java.util.*; public class Main{ //Main method public static void main(String args[]){ //initialized a number int num = 6549; //printing the given number System.out.println("Given number: "+num); // Checks if the number is less than 100 if(num<100){ System.out.println("The number is not a bouncy number"); } // Boolean values to check if the digits are sorted boolean increasing = true, decreasing = true; // Storing the number in a temporary variable and storing the first digit int temp = num, prev = num % 10; // Loops until the number becomes zero while(temp != 0){ // Checks if it follows increasing order or not if(temp%10>prev){ increasing = false; break; } // Gets the last digit of the number prev = temp % 10; // Removes the last digit temp /= 10; } // Resets the value temp = num; prev = num % 10; while(temp != 0){ // Checks if it follows decreasing order or not if(temp%10<prev){ decreasing = false; break; } // Gets the last digit of the number prev = temp % 10; // Removes the last digit temp /= 10; } // Prints the result if(!increasing&&!decreasing) System.out.println("The number is a bouncy number"); else System.out.println(", The number is not a bouncy number"); } }

輸出

Given number: 6549
The number is a bouncy number
.

方法2:使用使用者自定義方法和charAt()方法

在這種方法中,將要求使用者輸入一個整數值,並將此數字作為引數傳遞給使用者自定義方法,然後在方法內部使用演算法來檢查該數是否為迴文數。

示例

public class Main{ // Checks if the digits are in increasing order static boolean isIncreasing(int num){ // Converts the number to a string String s = Integer.toString(num); char dig; boolean result = true; // Loops over all the digits for(int i = 0; i < s.length()-1; i++){ // Stores the digit at i th location dig = s.charAt(i); // Compares the digit with its next location if(dig > s.charAt(i+1)){ // Sets the result to false and breaks out of the loop result = false; break; } } return result; } // Checks if the digits are in decreasing order static boolean isDecreasing(int num){ // Converts the number to a string String s = Integer.toString(num); char dig; boolean result = true; // Loops over all the digits for(int i = 0; i < s.length()-1; i++){ // Stores the digit at i th location dig = s.charAt(i); // Compares the digit with its next location if(dig < s.charAt(i+1)){ // Sets the result to false and breaks out of the loop result = false; break; } } return result; } //user defined method to check bouncy number static boolean bouncyNum(int num){ // Checks if the number is less than 100 if(num<100){ System.out.println("The number is not a bouncy number"); } // Checks for sorted digits using the functions if(!isIncreasing(num)&&!isDecreasing(num)) return true; else return false; } //main method public static void main(String args[]){ //initialized a number int num = 150; //printing the given number System.out.println("Given number: "+num); // Calls the user defined method and stores the result in res variable boolean res = bouncyNum(num); // Prints the result if(res) System.out.println("The number is a bouncy number"); else System.out.println("The number is not a bouncy number"); } }

輸出

Given number: 150
The number is a bouncy number

在本文中,我們探討了如何使用不同的方法在Java中檢查一個數是否為迴文數。

更新於:2022年10月27日

3K+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.