国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
Java 中回文背后的邏輯
如何使用各種方法測試回文?
1.使用for循環(huán)檢查回文數(shù)的程序
2.使用 While 循環(huán)檢查回文數(shù)的程序
3.使用庫方法檢查回文數(shù)的程序(對于字符串)
結(jié)論
首頁 Java java教程 Java 中的回文

Java 中的回文

Aug 30, 2024 pm 04:26 PM
java

如果字符串或數(shù)字顛倒后仍保持不變,則稱其為回文。例如,“MADAM”是一個回文字符串,因?yàn)榧词诡嵉惯^來也會拼寫為“MADAM”。但在“LUCKY”的情況下,該字符串不是回文,因?yàn)榉崔D(zhuǎn)后它是“YKCUL”。一些回文數(shù)是 365563、48984、12321、171、88、90009、343,一些回文字符串是 MADAM、MALAYALAM、LOL、DAD、MOM、C++&++C 等。 接下來讓我們看看回文的邏輯和實(shí)現(xiàn)。在本主題中,我們將學(xué)習(xí) Java 中的回文。

開始您的免費(fèi)軟件開發(fā)課程

網(wǎng)絡(luò)開發(fā)、編程語言、軟件測試及其他

Java 中回文背后的邏輯

為了檢查一個數(shù)字是否是回文數(shù),可以使用以下算法。

  • 輸入一個字符串或數(shù)字,需要檢查它是否是回文。

例如,讓我們輸入數(shù)字 353。

  • 獲取輸入數(shù)字并將其復(fù)制到臨時變量

353-> temp

  • 使用 for、while 或您選擇的任何方法反轉(zhuǎn)它。

Reversednumber: rev=353

  • 比較輸入的數(shù)字和反轉(zhuǎn)的數(shù)字。

如果它們相同,則該數(shù)字被稱為回文數(shù)。

否則,該數(shù)字不是回文數(shù)。

If(inputnum==rev)
{ then palindrome }
Else not palindrome

如何使用各種方法測試回文?

有幾種方法可以檢查給定的輸入數(shù)字是否是回文。

  • For 循環(huán)
  • While 循環(huán)
  • 庫方法(用于字符串)

讓我們詳細(xì)研究每一個:

1.使用for循環(huán)檢查回文數(shù)的程序

代碼:

//Java program to check whether a String is a Palindrome or not using For Loop
import java.util.*;
public class PalindromeNumberExample {
//main method
public static void main(String[] args) {
int r=0 ; //reversed Integer
int rem, num; //remainder and original number
Scanner s = new Scanner(System.in);
System.out.print("Enter number that has to be checked:");
num = s.nextInt();
//Store the number in a temporary variable
int temp = num;
//loop to find the reverse of a number
for( ;num != 0; num /= 10 )
{
rem = num % 10; // find the modulus of the number when divided by 10
r = r * 10 + rem;
}
//check whether the original and reversed numbers are equal
if (temp == r)
{
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are equal " + temp + " is a palindrome number");
}
else
{
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are not equal " + temp + " is not a palindrome number");
}
}
}

輸出 1:

Java 中的回文

這里,由于353顛倒后是一樣的,所以被認(rèn)為是回文。

輸出 2:

Java 中的回文

這里,由于 234 顛倒后仍然不一樣,因此不被視為回文。

2.使用 While 循環(huán)檢查回文數(shù)的程序

代碼:

//Java program to check whether a number is a Palindrome or not using While Loop
import java.util.*;
public class PalindromeNumberExample {
public static void main(String[] args) {
int r=0, rem, num;
Scanner s = new Scanner(System.in);
System.out.print("Enter number that has to be checked:");
num = s.nextInt();
//Store the number in a temporary variable
int temp = num;
//loop to find the reverse of a number
while( num != 0 )
{
rem= num % 10;
r= r * 10 + rem;
num=num/10;
}
//check whether the original and reversed numbers are equal
if (temp == r)
{
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are equal " + temp + " is a palindrome number");
}
else
{
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are not equal " + temp + " is not a palindrome number");
}
}
}

輸出 1:

Java 中的回文

輸出 2:

Java 中的回文

3.使用庫方法檢查回文數(shù)的程序(對于字符串)

代碼:

//Java program to check whether a String is a Palindrome or not using Library method
import java.util.*;
public class PalindromeNumberExample {
//Function to check whether the string is palindrome or not
public static void PalindromeCheck(String str)
{
// reverse the input String
String rev = new StringBuffer(str).reverse().toString();
// checks whether the string is palindrome or not
if (str.equals(rev))
{
System.out.println("input string is :" + str);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are equal, "+ str +" is a palindrome");
}
else
{
System.out.println("input string is :" + str);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are not equal, "+ str +" is not a palindrome");
}
}
public static void main (String[] args)
{
PalindromeCheck("MALAYALAM");
}
}

輸出:

Java 中的回文

這里,輸入字符串是在程序本身中傳遞的。

要檢查字符串是否是回文,還可以使用以下程序。

代碼:

//Java program to check whether a String is a Palindrome or not
import java.util.*;
public class PalindromeNumberExample {
public static void main(String args[])
{
String st, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string that has to be checked:");
st = sc.nextLine();
int len = st.length(); //length of the string
for ( int i = len- 1; i >= 0; i-- )
rev = rev + st.charAt(i);
if (st.equals(rev))
{
System.out.println("input string is :" + st);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are equal, "+ st +" is a palindrome");
}
else
{
System.out.println("input string is :" + st);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are not equal, "+ st +" is not a palindrome");
}
}
}

輸出:

Java 中的回文

結(jié)論

如果一個數(shù)字即使顛倒也保持不變,則稱為回文數(shù)。回文也可以在字符串中檢查。回文數(shù)和字符串有 MOM、MALAYALAM、DAD、LOL、232、1331 等。本文檔涵蓋了回文數(shù)的幾個方面,如算法、方法、實(shí)現(xiàn)等。

以上是Java 中的回文的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

選擇特定的列|性能優(yōu)化 選擇特定的列|性能優(yōu)化 Jun 27, 2025 pm 05:46 PM

1.FetchingAllColumnSInCreaseSemory,Network和PropoSessingOverHead.2.unnectaryDatareTrievalPreventSefefectivefective.1.FetchingAllColumnSInCreaseSemory,選擇innyleneedcolumnsimprovesmproveSimproveSimproveSranceByreducingReSouranceByReDucingRessourceusage

Java中的'枚舉”類型是什么? Java中的'枚舉”類型是什么? Jul 02, 2025 am 01:31 AM

Java中的枚舉(enum)是一種特殊的類,用于表示固定數(shù)量的常量值。1.使用enum關(guān)鍵字定義;2.每個枚舉值都是該枚舉類型的公共靜態(tài)最終實(shí)例;3.可以包含字段、構(gòu)造函數(shù)和方法,為每個常量添加行為;4.可在switch語句中使用,支持直接比較,并提供name()、ordinal()、values()和valueOf()等內(nèi)置方法;5.枚舉可提升代碼的類型安全性、可讀性和靈活性,適用于狀態(tài)碼、顏色或星期等有限集合場景。

將語義結(jié)構(gòu)應(yīng)用于html的文章,部分和旁邊 將語義結(jié)構(gòu)應(yīng)用于html的文章,部分和旁邊 Jul 05, 2025 am 02:03 AM

在HTML中合理使用語義化標(biāo)簽?zāi)芴嵘撁娼Y(jié)構(gòu)清晰度、可訪問性和SEO效果。1.用于獨(dú)立內(nèi)容區(qū)塊,如博客文章或評論,需保持自包含性;2.用于歸類相關(guān)內(nèi)容,通常包含標(biāo)題,適用于頁面不同模塊;3.用于與主內(nèi)容相關(guān)但非核心的輔助信息,如側(cè)邊欄推薦或作者簡介。實(shí)際開發(fā)中應(yīng)結(jié)合、等標(biāo)簽,避免過度嵌套,保持結(jié)構(gòu)簡潔,并通過開發(fā)者工具驗(yàn)證結(jié)構(gòu)合理性。

什么是JDK? 什么是JDK? Jun 25, 2025 pm 04:05 PM

JDK(JavaDevelopmentKit)是用于開發(fā)Java應(yīng)用程序和小程序的軟件開發(fā)環(huán)境,包含編譯、調(diào)試和運(yùn)行Java程序所需的工具與庫。其核心組件包括Java編譯器(javac)、Java運(yùn)行時環(huán)境(JRE)、Java解釋器(java)、調(diào)試器(jdb)、文檔生成工具(javadoc)及打包工具(如jar和jmod)。開發(fā)者需要JDK來編寫、編譯Java代碼,并借助IDE進(jìn)行開發(fā);沒有JDK則無法構(gòu)建或修改Java應(yīng)用??赏ㄟ^在終端輸入javac-version和java-version

Java設(shè)置指南的VSCODE調(diào)試器 Java設(shè)置指南的VSCODE調(diào)試器 Jul 01, 2025 am 12:22 AM

配置Java調(diào)試環(huán)境在VSCode上的關(guān)鍵步驟包括:1.安裝JDK并驗(yàn)證;2.安裝JavaExtensionPack和DebuggerforJava插件;3.創(chuàng)建并配置launch.json文件,指定mainClass和projectName;4.設(shè)置正確的項(xiàng)目結(jié)構(gòu),確保源碼路徑和編譯輸出正確;5.使用調(diào)試技巧如Watch、F8/F10/F11快捷鍵及處理常見問題如類找不到或JVM附加失敗的方法。

XML規(guī)則:避免的常見錯誤 XML規(guī)則:避免的常見錯誤 Jun 22, 2025 am 12:09 AM

避免XML錯誤的方法包括:1.確保元素正確嵌套,2.轉(zhuǎn)義特殊字符。正確嵌套避免解析錯誤,而轉(zhuǎn)義字符防止文檔損壞,使用XML編輯器可幫助維護(hù)結(jié)構(gòu)完整性。

Windows搜索欄未輸入 Windows搜索欄未輸入 Jul 02, 2025 am 10:55 AM

Windows搜索欄無法輸入文字時,常見的解決方法有:1.重啟資源管理器或電腦,可打開任務(wù)管理器重新啟動“Windows資源管理器”進(jìn)程,或直接重啟設(shè)備;2.切換或卸載輸入法,嘗試使用英文輸入法或微軟自帶輸入法,排除第三方輸入法沖突;3.運(yùn)行系統(tǒng)文件檢查工具,在命令提示符中執(zhí)行sfc/scannow命令修復(fù)系統(tǒng)文件;4.重置或重建搜索索引,通過“控制面板”中的“索引選項(xiàng)”進(jìn)行重建。通常先從簡單步驟開始排查,多數(shù)問題可以逐步解決。

如何為Java開發(fā)設(shè)置VS代碼? 如何為Java開發(fā)設(shè)置VS代碼? Jun 29, 2025 am 12:23 AM

要使用VSCode進(jìn)行Java開發(fā),需安裝必要擴(kuò)展、配置JDK和設(shè)置工作區(qū)。1.安裝JavaExtensionPack,包含語言支持、調(diào)試集成、構(gòu)建工具和代碼補(bǔ)全功能;可選裝JavaTestRunner或SpringBoot擴(kuò)展包。2.安裝至少JDK17,并通過java-version和javac-version驗(yàn)證;設(shè)置JAVA_HOME環(huán)境變量,或在VSCode底部狀態(tài)欄切換多個JDK。3.打開項(xiàng)目文件夾后,確保項(xiàng)目結(jié)構(gòu)正確并啟用自動保存,調(diào)整格式化規(guī)則、啟用代碼檢查,并配置編譯任務(wù)以優(yōu)化開

See all articles