Home > Java > Javagetting Started > body text

Java determines whether it is a palindrome

爱喝马黛茶的安东尼
Release: 2019-11-15 09:19:56
Original
3281 people have browsed it

Java determines whether it is a palindrome

1. Invert the strings and compare them one by one. The implementation is as follows:

 public class HuiWenTest {
/**
* @SERLIN
*/
public static void main(String[] args) {
String str = "";
System.out.println("请输入一个字符串");
Scanner input = new Scanner(System.in);
str = input.next();
 
StringBuffer sb = new StringBuffer(str);
sb.reverse();// 将Str中的字符串倒置
 
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == sb.charAt(i)) {
count++;
}
}
if (count == str.length()) {
System.out.println("此字符串是一个回文字符串");
} else {
System.out.println("此字符串不是一个回文字符串");
}
}
}
Copy after login

2. Invert the string and create a new string for direct comparison. The implementation is as follows:

public class HuiWenTest2 {
/**
* @SERLIN
*/
public static void main(String[] args) {
System.out.println("请输入一个字符串");
Scanner input = new Scanner(System.in);
String str = input.next();
StringBuilder sb=new StringBuilder(str);
sb.reverse();//将str倒置的方法
String newStr=new String(sb);
if(str.equals(newStr)){
System.out.println(str+"是回文字符串");
}else{
System.out.println(str+"不是回文字符串");
}
}
}
Copy after login

3. Use the interception method to compare the string and the implementation is as follows:

public class HuiWenTest3 {
/**
* @SERLIN
*/
public static void main(String[] args) {
System.out.println("请输入一个字符串");
Scanner input = new Scanner(System.in);
String str = input.next();
int count = 0;
for (int i = 0; i < str.length() / 2; i++) {
if ((str.substring(i, i + 1)).equals(str.substring(str.length() - 1- i, str.length() - i))) {
count++;
}
}
if (count == str.length() / 2) {
System.out.println("是回文字符串");
}else{
System.out.println("不是回文字符串");
}
}
}
Copy after login

4. Judgment of palindrome numbers (judgment of pure numbers) is implemented as follows:

public class HuiWenNum {
/**
* @SERLIN
*/
public static void main(String[] args) {
int n;
System.out.println("请输入一个整数:"); 
// 如果结果为回文数,跳出循环
while (true) {
Scanner InpuNum = new Scanner(System.in);
n = InpuNum.nextInt();
if (isHuiWen(n)) {
System.out.println(n + "是回文数!");
break;
} else {
System.out.println(n + "不是回文数!");
}
}
}

 
// 判断的数字是否是回文数
public static boolean isHuiWen(int n) {
int m = reverse(n);
if (m == n) {
return true;
} else {
return false;
}
}

 
// 将输入的数字进行倒置,以便进行判断是否是回文数
public static int reverse(int n) {
int temp = 0;// 临时变量
int j = 0;// 倒置后的数字
temp = n;// 将输入的数字赋值给临时变量
while (temp != 0) {
j = j * 10 + temp % 10;
temp /= 10;
}
return j;
}
}
Copy after login

php Chinese website, a large number of free Java introductory tutorials, welcome online study!

The above is the detailed content of Java determines whether it is a palindrome. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!