ใน Java จะตรวจสอบได้อย่างไรว่า Number/String เป็น Palindrome หรือไม่?
เผยแพร่แล้ว: 2021-04-13
เลขพาลินโดรมคืออะไร?
เลขพาลินโดรมคือตัวเลข (เช่น 24842) ที่ remains the same
เมื่อกลับหลัก
ต่อไปนี้เป็นชื่ออื่นๆ:
- ตัวเลขพาลินโดรม
- แป้นตัวเลข
ในบทช่วยสอนนี้ เราจะพูดถึงขั้นตอนในการตรวจสอบว่า Number and String เป็น Palindrome หรือไม่
หากคุณมีคำถามใด ๆ ด้านล่างแสดงว่าคุณมาถูกที่แล้ว:
- โปรแกรม Java สำหรับตรวจสอบว่าตัวเลขเป็น Palindrome หรือไม่
- คำจำกัดความของตัวเลขพาลินโดรม
- โปรแกรม Java เพื่อตรวจสอบว่าสตริงที่ระบุเป็น Palindrome หรือไม่
ขั้นตอน – มาเริ่มกันเลย
- เขียนโปรแกรม CrunchifyFindPalindromeNumber.java
- เราจะสร้าง 3 วิธี:
- crunchifyFindPalindromeUsingForLoop()
- กระทืบFindPalindromeUsingWhileLoop()
- กระทืบFindPalindromeForString()
- พิมพ์ผล
CrunchifyFindPalindromeNumber.java
เราจะใช้ AtomicInteger ที่นี่ในโปรแกรม Java นี้ เป็นค่า int ที่อาจมีการปรับปรุงแบบปรมาณู ดูข้อกำหนดของ VarHandle สำหรับคำอธิบายคุณสมบัติของการเข้าถึงแบบอะตอม
AtomicInteger ใช้ในแอปพลิเคชันต่างๆ เช่น ตัวนับที่เพิ่มขึ้นของอะตอม และไม่สามารถใช้แทนจำนวนเต็มได้ อย่างไรก็ตาม คลาสนี้ขยาย Number เพื่ออนุญาตให้เข้าถึงแบบเดียวกันโดยเครื่องมือและยูทิลิตี้ที่จัดการกับคลาสที่ใช้ตัวเลข
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
package crunchify . com . java . tutorials ; import java . util . concurrent . atomic . AtomicInteger ; /** * @author Crunchify.com * In Java How to Check if Number/String is Palindrome or not? */ public class CrunchifyFindPalindromeNumber { public static void main ( String [ ] args ) { crunchifyFindPalindromeUsingForLoop ( 24642 ) ; crunchifyFindPalindromeUsingWhileLoop ( 34567 ) ; crunchifyFindPalindromeUsingForLoop ( 987656789 ) ; crunchifyFindPalindromeUsingWhileLoop ( 555555 ) ; crunchifyFindPalindromeUsingForLoop ( 1 ) ; crunchifyFindPalindromeUsingWhileLoop ( 989 ) ; crunchifyPrint ( "" ) ; crunchifyFindPalindromeForString ( "abcdefedcba" ) ; crunchifyFindPalindromeForString ( "abcdefghij" ) ; } // Method 1: Find Palindrome for String private static void crunchifyFindPalindromeForString ( String crunchifyString ) { // reverse(): AbstractStringBuilder Causes this character sequence to be replaced by the reverse of the sequence. // If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. // Thus, the order of the high-low surrogates is never reversed. Let n be the character length of this character sequence // (not the length in char values) just prior to execution of the reverse method. Then the character at index k in the new character // sequence is equal to the character at index n-k-1 in the old character sequence. String crunchifyReverse = new StringBuffer ( crunchifyString ) . reverse ( ) . toString ( ) ; // checks whether the string is palindrome or not if ( crunchifyString . equals ( crunchifyReverse ) ) crunchifyPrint ( crunchifyString + " --> Yes, it is a palindrome String." ) ; else crunchifyPrint ( crunchifyString + " --> No, it is not a palindrome String." ) ; } // Simple Print Utility private static void crunchifyPrint ( String crunchifyString ) { System . out . println ( crunchifyString ) ; } // Method 2: Find Palindrome for Integer using For Loop private static void crunchifyFindPalindromeUsingForLoop ( int number ) { // AtomicInteger: An int value that may be updated atomically. // See the VarHandle specification for descriptions of the properties of atomic accesses. // An AtomicInteger is used in applications such as atomically incremented counters, and cannot be used as a replacement for an Integer. // However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes. AtomicInteger reverseNumber = new AtomicInteger ( ) ; AtomicInteger crunchifyRemainder = new AtomicInteger ( ) ; int crunchifyOriginal ; AtomicInteger crunchifyNumber = new AtomicInteger ( number ) ; crunchifyOriginal = crunchifyNumber . get ( ) ; // reversed integer is stored in variable // updateAndGet Atomically updates (with memory effects as specified by VarHandle.compareAndSet) the current value with the results of // applying the given function, returning the updated value. // The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. for ( ; crunchifyNumber . get ( ) ! = 0 ; crunchifyNumber . updateAndGet ( v - > v / 10 ) ) { crunchifyRemainder . set ( crunchifyNumber . get ( ) % 10 ) ; reverseNumber . set ( reverseNumber . get ( ) * 10 + crunchifyRemainder . get ( ) ) ; } // Palindrome is a number that remains the same when its digits are reversed. if ( crunchifyOriginal == reverseNumber . get ( ) ) crunchifyPrint ( crunchifyOriginal + " --> Yes, it is a palindrome Number." ) ; else crunchifyPrint ( crunchifyOriginal + " --> No, it is not a palindrome Number." ) ; } // Method 3: Find Palindrome for Integer using While Loop private static void crunchifyFindPalindromeUsingWhileLoop ( int number ) { AtomicInteger reverseNumber = new AtomicInteger ( ) ; AtomicInteger crunchifyRemainder = new AtomicInteger ( ) ; int crunchifyOriginal ; int crunchifyNumber = number ; crunchifyOriginal = crunchifyNumber ; while ( crunchifyNumber ! = 0 ) { crunchifyRemainder . set ( crunchifyNumber % 10 ) ; // set() Sets the value to newValue, with memory effects as specified by VarHandle. // get() Returns the current value, with memory effects as specified by VarHandle.getVolatile. reverseNumber . set ( reverseNumber . get ( ) * 10 + crunchifyRemainder . get ( ) ) ; crunchifyNumber /= 10 ; } // Palindrome is a number that remains the same when its digits are reversed. if ( crunchifyOriginal == reverseNumber . get ( ) ) crunchifyPrint ( crunchifyOriginal + " --> Yes, is a palindrome Number." ) ; else crunchifyPrint ( crunchifyOriginal + " --> No, is not a palindrome Number." ) ; } } |
เพียงเรียกใช้โปรแกรมด้านบนเป็นแอปพลิเคชัน Java และคุณควรได้รับผลลัพธ์ดังนี้

ผลลัพธ์คอนโซล Eclipse / IntelliJ IDEA
1 2 3 4 5 6 7 8 9 10 11 |
24642 -- > Yes , it is a palindrome Number . 34567 -- > No , is not a palindrome Number . 987656789 -- > Yes , it is a palindrome Number . 555555 -- > Yes , is a palindrome Number . 1 -- > Yes , it is a palindrome Number . 989 -- > Yes , is a palindrome Number . abcdefedcba -- > Yes , it is a palindrome String . abcdefghij -- > No , it is not a palindrome String . Process finished with exit code 0 |
แจ้งให้เราทราบหากคุณประสบปัญหาในการใช้งานโปรแกรมข้างต้น