亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
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”是一個回文字符串,因為即使顛倒過來也會拼寫為“MADAM”。但在“LUCKY”的情況下,該字符串不是回文,因為反轉(zhuǎn)后它是“YKCUL”。一些回文數(shù)是 365563、48984、12321、171、88、90009、343,一些回文字符串是 MADAM、MALAYALAM、LOL、DAD、MOM、C++&++C 等。 接下來讓我們看看回文的邏輯和實現(xiàn)。在本主題中,我們將學(xué)習(xí) Java 中的回文。

開始您的免費軟件開發(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)
  • 庫方法(用于字符串)

讓我們詳細研究每一個:

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顛倒后是一樣的,所以被認為是回文。

輸出 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ù)?;匚囊部梢栽谧址袡z查。回文數(shù)和字符串有 MOM、MALAYALAM、DAD、LOL、232、1331 等。本文檔涵蓋了回文數(shù)的幾個方面,如算法、方法、實現(xiàn)等。

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

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權(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

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

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

Dreamweaver CS6

Dreamweaver CS6

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

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

Laravel Raw SQL查詢示例 Laravel Raw SQL查詢示例 Jul 29, 2025 am 02:59 AM

Laravel支持使用原生SQL查詢,但應(yīng)優(yōu)先使用參數(shù)綁定以確保安全;1.使用DB::select()執(zhí)行帶參數(shù)綁定的SELECT查詢,防止SQL注入;2.使用DB::update()執(zhí)行UPDATE操作并返回影響行數(shù);3.使用DB::insert()插入數(shù)據(jù);4.使用DB::delete()刪除數(shù)據(jù);5.使用DB::statement()執(zhí)行如CREATE、ALTER等無結(jié)果集的SQL語句;6.推薦在QueryBuilder中使用whereRaw、selectRaw等方法結(jié)合原生表達式以提升安

使用Junit 5和Mockito在Java進行單位測試和嘲笑 使用Junit 5和Mockito在Java進行單位測試和嘲笑 Jul 29, 2025 am 01:20 AM

使用JUnit5和Mockito能有效隔離依賴進行單元測試,1.通過@Mock創(chuàng)建模擬對象,@InjectMocks注入被測實例,@ExtendWith啟用Mockito擴展;2.使用when().thenReturn()定義模擬行為,verify()驗證方法調(diào)用次數(shù)與參數(shù);3.可模擬異常場景并驗證錯誤處理;4.推薦構(gòu)造函數(shù)注入、避免過度模擬、保持測試原子性;5.使用assertAll()合并斷言,@Nested組織測試場景,從而提升測試可維護性和可靠性。

以身作則 以身作則 Jul 29, 2025 am 04:10 AM

Go泛型從1.18開始支持,用于編寫類型安全的通用代碼。1.泛型函數(shù)PrintSlice[Tany](s[]T)可打印任意類型切片,如[]int或[]string。2.通過類型約束Number限制T為int、float等數(shù)字類型,實現(xiàn)Sum[TNumber](slice[]T)T安全求和。3.泛型結(jié)構(gòu)體typeBox[Tany]struct{ValueT}可封裝任意類型值,配合NewBox[Tany](vT)*Box[T]構(gòu)造函數(shù)使用。4.為Box[T]添加Set(vT)和Get()T方法,無需

CSS桌面固定示例 CSS桌面固定示例 Jul 29, 2025 am 04:28 AM

table-layout:fixed會強制表格列寬由第一行單元格寬度決定,避免內(nèi)容影響布局。1.設(shè)置table-layout:fixed并指定表格寬度;2.為第一行th/td設(shè)置具體列寬比例;3.配合white-space:nowrap、overflow:hidden和text-overflow:ellipsis控制文本溢出;4.適用于后臺管理、數(shù)據(jù)報表等需穩(wěn)定布局和高性能渲染的場景,能有效防止布局抖動并提升渲染效率。

Python JSON負載示例 Python JSON負載示例 Jul 29, 2025 am 03:23 AM

json.loads()用于將JSON字符串解析為Python數(shù)據(jù)結(jié)構(gòu),1.輸入必須是雙引號包裹的字符串且布爾值為true/false;2.支持null→None、對象→dict、數(shù)組→list等自動轉(zhuǎn)換;3.常用于處理API返回的JSON字符串,如response_string經(jīng)json.loads()解析后可直接訪問嵌套數(shù)據(jù),使用時需確保JSON格式正確,否則會拋出異常。

MongoDB的索引策略 MongoDB的索引策略 Jul 29, 2025 am 01:05 AM

Choosetheappropriateindextypebasedonusecase,suchassinglefield,compound,multikey,text,geospatial,orTTLindexes.2.ApplytheESRrulewhencreatingcompoundindexesbyorderingfieldsasequality,sort,thenrange.3.Designindexestosupportcoveredqueriesbyincludingallque

Java項目管理Maven的開發(fā)人員指南 Java項目管理Maven的開發(fā)人員指南 Jul 30, 2025 am 02:41 AM

Maven是Java項目管理和構(gòu)建的標準工具,答案在于它通過pom.xml實現(xiàn)項目結(jié)構(gòu)標準化、依賴管理、構(gòu)建生命周期自動化和插件擴展;1.使用pom.xml定義groupId、artifactId、version和dependencies;2.掌握核心命令如mvnclean、compile、test、package、install和deploy;3.利用dependencyManagement和exclusions管理依賴版本與沖突;4.通過多模塊項目結(jié)構(gòu)組織大型應(yīng)用并由父POM統(tǒng)一管理;5.配

Python通過參考示例 Python通過參考示例 Jul 29, 2025 am 12:31 AM

Python中函數(shù)傳參是“傳遞對象引用”,即1.對于可變對象(如列表、字典),函數(shù)內(nèi)進行原地修改(如append、賦值切片)會直接影響原對象;2.對于不可變對象(如整數(shù)、字符串),函數(shù)內(nèi)無法改變原對象,重新賦值只會創(chuàng)建新對象;3.參數(shù)傳遞的是引用的副本,若在函數(shù)內(nèi)重新綁定變量(如lst=[...]),則斷開與原對象的連接,不影響外部變量。因此,修改可變對象會影響原數(shù)據(jù),而不可變對象和重新賦值則不會,這解釋了為何列表在函數(shù)內(nèi)修改后外部可見,而整數(shù)變化僅限局部。

See all articles