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

目錄
Java 中回文背後的邏輯
如何使用各種方法測試回文?
1.使用for迴圈檢查回文數(shù)的程式
2.使用 While 迴圈檢查回文數(shù)的程式
3.使用函式庫方法檢查回文數(shù)的程式(對(duì)於字串)
結(jié)論
首頁 Java java教程 Java 中的回文

Java 中的回文

Aug 30, 2024 pm 04:26 PM
java

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

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

網(wǎng)頁開發(fā)、程式語言、軟體測試及其他

Java 中回文背後的邏輯

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

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

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

  • 取得輸入數(shù)字並將其複製到臨時(shí)變數(shù)

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)
  • 函式庫方法(用於字串)

讓我們?cè)敿?xì)研究每一個(gè):

1.使用for迴圈檢查回文數(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 迴圈檢查回文數(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ù)的程式(對(duì)於字串)

代碼:

//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é)論

如果一個(gè)數(shù)字即使顛倒也保持不變,則稱為回文數(shù)?;匚囊部梢栽谧执袡z查?;匚臄?shù)和字串有 MOM、MALAYALAM、DAD、LOL、232、1331 等。本文檔涵蓋了回文數(shù)的幾個(gè)方面,如演算法、方法、實(shí)作等。

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

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

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

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版

神級(jí)程式碼編輯軟體(SublimeText3)

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

在HTML中合理使用語義化標(biāo)籤能提升頁面結(jié)構(gòu)清晰度、可訪問性和SEO效果。 1.用於獨(dú)立內(nèi)容區(qū)塊,如博客文章或評(píng)論,需保持自包含性;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)合理性。

請(qǐng)求的操作需要高程窗戶 請(qǐng)求的操作需要高程窗戶 Jul 04, 2025 am 02:58 AM

遇到“此操作需要提升權(quán)限”提示時(shí),說明你需要管理員權(quán)限才能繼續(xù)。解決方法包括:1.右鍵選擇“以管理員身份運(yùn)行”程序或設(shè)置快捷方式始終以管理員身份運(yùn)行;2.檢查當(dāng)前賬戶是否為管理員賬戶,若不是則切換或請(qǐng)求管理員協(xié)助;3.用管理員權(quán)限打開命令提示符或PowerShell執(zhí)行相關(guān)命令;4.在必要時(shí)通過獲取文件所有權(quán)或修改註冊(cè)表等手段繞過限制,但此類操作需謹(jǐn)慎並充分了解風(fēng)險(xiǎn)。確認(rèn)權(quán)限身份並嘗試上述方法通??山鉀Q問題。

Java中可呼叫和可運(yùn)行的差異 Java中可呼叫和可運(yùn)行的差異 Jul 04, 2025 am 02:50 AM

Callable和Runnable在Java中主要有三點(diǎn)區(qū)別。第一,Callable的call()方法可以返回結(jié)果,適合需要返回值的任務(wù),如Callable;而Runnable的run()方法無返回值,適用於無需返回的任務(wù),如日誌記錄。第二,Callable允許拋出checked異常,便於錯(cuò)誤傳遞;而Runnable必須在內(nèi)部處理異常。第三,Runnable可直接傳給Thread或ExecutorService,而Callable只能提交給ExecutorService,並返回Future對(duì)像以

探索Java中不同的同步機(jī)制 探索Java中不同的同步機(jī)制 Jul 04, 2025 am 02:53 AM

Javaprovidesmultiplesynchronizationtoolsforthreadsafety.1.synchronizedblocksensuremutualexclusionbylockingmethodsorspecificcodesections.2.ReentrantLockoffersadvancedcontrol,includingtryLockandfairnesspolicies.3.Conditionvariablesallowthreadstowaitfor

Java Classloader在內(nèi)部如何工作 Java Classloader在內(nèi)部如何工作 Jul 06, 2025 am 02:53 AM

Java的類加載機(jī)制通過ClassLoader實(shí)現(xiàn),其核心工作流程分為加載、鏈接和初始化三個(gè)階段。加載階段由ClassLoader動(dòng)態(tài)讀取類的字節(jié)碼並創(chuàng)建Class對(duì)象;鏈接包括驗(yàn)證類的正確性、為靜態(tài)變量分配內(nèi)存及解析符號(hào)引用;初始化則執(zhí)行靜態(tài)代碼塊和靜態(tài)變量賦值。類加載採用雙親委派模型,優(yōu)先委託父類加載器查找類,依次嘗試Bootstrap、Extension和ApplicationClassLoader,確保核心類庫安全且避免重複加載。開發(fā)者可自定義ClassLoader,如URLClassL

有效處理常見的Java例外 有效處理常見的Java例外 Jul 05, 2025 am 02:35 AM

Java異常處理的關(guān)鍵在於區(qū)分checked和unchecked異常並合理使用try-catch、finally及日誌記錄。 1.checked異常如IOException需強(qiáng)制處理,適用於可預(yù)期的外部問題;2.unchecked異常如NullPointerException通常由程序邏輯錯(cuò)誤引起,屬於運(yùn)行時(shí)錯(cuò)誤;3.捕獲異常時(shí)應(yīng)具體明確,避免籠統(tǒng)捕獲Exception;4.推薦使用try-with-resources自動(dòng)關(guān)閉資源,減少手動(dòng)清理代碼;5.異常處理中應(yīng)結(jié)合日誌框架記錄詳細(xì)信息,便於後

現(xiàn)代爪哇的異步編程技術(shù) 現(xiàn)代爪哇的異步編程技術(shù) Jul 07, 2025 am 02:24 AM

Java支持異步編程的方式包括使用CompletableFuture、響應(yīng)式流(如ProjectReactor)以及Java19 中的虛擬線程。 1.CompletableFuture通過鍊式調(diào)用提升代碼可讀性和維護(hù)性,支持任務(wù)編排和異常處理;2.ProjectReactor提供Mono和Flux類型實(shí)現(xiàn)響應(yīng)式編程,具備背壓機(jī)制和豐富的操作符;3.虛擬線程減少並發(fā)成本,適用於I/O密集型任務(wù),與傳統(tǒng)平臺(tái)線程相比更輕量且易於擴(kuò)展。每種方式均有適用場景,應(yīng)根據(jù)需求選擇合適工具並避免混合模型以保持簡潔性

Java中'靜態(tài)”關(guān)鍵字的目的是什麼? Java中'靜態(tài)”關(guān)鍵字的目的是什麼? Jul 05, 2025 am 02:36 AM

靜態(tài)關(guān)鍵字在Java中用於創(chuàng)建屬於類本身的變量和方法,而非類的實(shí)例。 1.靜態(tài)變量被所有類的實(shí)例共享,適用於存儲(chǔ)所有對(duì)象共有的數(shù)據(jù),如Student類中的schoolName。 2.靜態(tài)方法屬於類,不依賴對(duì)象,常用於工具函數(shù),如Math.sqrt(),且只能訪問其他靜態(tài)成員。 3.靜態(tài)代碼塊用於在類加載時(shí)執(zhí)行初始化操作,如加載庫或設(shè)置日誌。 4.靜態(tài)內(nèi)部類可以獨(dú)立於外部類實(shí)例化,但無法訪問外部類的非靜態(tài)成員。合理使用static能有效管理類級(jí)別的資源和行為。

See all articles