Ⅰ java 判断回文字符串有多少和其中的最大字符
二、代码部分
1、全局变量
1 static String hws = "";
2 static int num = 0;
3 static String[] hw;
2、创建数组用于保存回文
复制代码
1 /**
2 * 创建数组保存所有的回文
3 *
4 * @return 返回一个String类型的数组
5 */
6 public static String[] createHw() {
7 return new String[num];
8 }
复制代码
3、将hws字符串更改为空字符
1 /**
2 * 将hws字符串更改为空字符
3 */
4 public static void hwsClose() {
5 hws = "";
6 }
4、判断该字符串中存在的回文的数量
复制代码
1 /**
2 * 判断该字符串中存在的回文的数量
3 *
4 * @param c
5 * 数组c ,c是用户输入的字符串转换为单个字符组成的数组
6 * @return
7 */
8
9 public static int judgeNumber(char[] c) {
10 for (int i = 0; i < c.length - 1; i++) {
11 if (c[i] == c[i + 1]) {
12 num++;
13 }
14 }
15 return num;
16 }
复制代码
5、第一次判断字符串中前后的数是否存在相同
复制代码
1 /**
2 * 第一次判断字符串中前后的数是否存在相同
3 *
4 * @param c
5 * 数组c ,c是用户输入的字符串转换为单个字符组成的数组
6 */
7
8 public static void judge(char[] c) {
9 judgeNumber(c);
10 if (num != 0) {
11 hw = createHw();
12 for (int i = 0; i < c.length - 1; i++) {
13 if (c[i] == c[i + 1]) {
14 hws = hws + c[i];
15 judge2(c, i, i + 1);
16 hw[--num] = hws;
17 hwsClose();
18 }
19 }
20 } else {
21 System.out.println("该字符串没有回文");
22 }
23 }
复制代码
6、进行二次判断以当前相同的字符为起点,分别以前下标向前和该后下标向后进行比较()
复制代码
1 /**
2 * 进行二次判断以当前相同的字符为起点,分别以前下标向前和该后下标向后进行比较()
3 *
4 * @param c
5 * 数组c ,c是用户输入的字符串转换为单个字符组成的数组
6 * @param i
7 * 数组前一个下标,该下标和后一个进行比较并且相同
8 * @param k
9 * 数组后一个下标,该下标和前一个进行比较并且相同
10 */
11
12 public static void judge2(char[] c, int i, int k) {
13 if (i - 1 >= 0 && k + 1 < c.length) {
14 if (c[i - 1] == c[k + 1]) {
15 hws = hws + c[i - 1];
16 judge2(c, i - 1, k + 1);
17 }
18 }
19 }
复制代码
7、输出所得的回文
复制代码
1 /**
2 * 获取所得的回文
3 *
4 * @param c
5 * 数组c ,c是用户输入的字符串转换为单个字符组成的数组
6 */
7
8 public static void outPalindrome(char[] c) {
9 judge(c);
10 if (hw!=null) {
11 for (int i = 0; i < hw.length; i++) {
12 System.out.println(reverse(hw[i])+hw[i]);
13 }
14 } else {
15 System.out.println("没有回文2");
16 }
17 }
复制代码
8、将最长的回文输出
复制代码
1 /**
2 * 将最长的回文输出
3 *
4 */
5
6 public static void longestPalindromes() {
7 String longest = null;
8 if (hw!=null) {
9 if (hw.length == 1) {
10 System.out.println(reverse(hw[0])+hw[0]);
11 } else {
12 for (int i = 0; i < hw.length - 1; i++) {
13 for (int j = 0; j < hw.length - 1 - i; j++) {
14 if (hw[j].length() > hw[j + 1].length()) {
15 longest = hw[j + 1];
16 hw[j + 1] = hw[j];
17 hw[j] = longest;
18 }
19 }
20 }
21 for (int i = 0; i < hw.length - 1; i++) {
22 if (hw[hw.length - 1].length() == hw[hw.length - i - 1].length()) {
23 System.out.println(reverse(hw[hw.length - i - 1])+hw[hw.length - i - 1]);
24 }
25 }
26 }
27 } else {
28 System.out.println("没有回文3");
29 }
30 }
Ⅱ java判断字符串是否为回文
equals方法接受的参数是字符串的,要用toString()。
提供建议如下:
1、进入if前,先输出刚才得到的用户输入值。
2、手工写段4位或5位回文数的算法,就是把用户的输入5位数字分别除1万、1千、1百、1十,得到每个位的数字,然后比较,看是否是回文数。把这个结果作为对比依据。
Ⅲ Java语言如何判断一个字符串是否回文
Java语言判断一个字符串是否回文可以通过下面的函数进行判断:
import java.util.Scanner;
public class huiwen
{public static void main(String args[])
{Scanner sca=new Scanner(System.in);
String str=sca.nextLine();
int a=str.length();
int middle=a/2,i=3;
//char c1=(char) a,c2=(char) a;
for(i=0;i<middle&&str.charAt(i)==str.charAt(a-1-i);i++)
{}
if(i<middle)
System.out.println(str+"不是回文");
else
System.out.println(str+"是回文");}}
c/c++判断一个字符串是否回文的方法:
#include <iostream>
#include <string>
using namespace std;
int main()
{string str; int i,j,l;int flag = 1;
while (cin >> str)
{ l = str.length(); for (i = 0,j = l-1; i <= j; i++,j--)
{ if (str[i] != str[j])
{ flag = 0;break; }}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
flag = true;}
return 0;}
Ⅳ 使用字符串判断它是不是回文数,用JAVA写
class huiwen{
public static void main(String[] args){
int k = args[0].length();
String n = args[0];
if(ishw(n,k))
System.out.println("\""+n+"\""+"是回文");
else
System.out.println("\""+n+"\""+"不是回文");
}
static boolean ishw(String n,int x){
int t=0;
for(int i=0;i<x/2;i++){
if(n.charAt(i)!=n.charAt(x-(i+1)))
t++;
}
if(t==0)
return true;
else
return false;
} }
Ⅳ java判断回文字符串几种简单的实现
import java.util.Scanner; public class Tststriu { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("输入字符串的个数:"); int n = sc.nextInt(); String[] a = new String[n]; for(int i = 0;i < n;++i){ System.out.println("输入第"+(i+1)+"个字符串:"); a[i] = sc.next(); } int sum = 0; String max = null; for(int i = 0;i < n;++i){ if(isString(a[i])) sum++; if(bijao(a[i],max)){ max = a[i]; } } System.out.println("回文字符串个数是:"+sum); System.out.println("最大字符串是:"+max); } public static boolean isString(String s){ int n = s.length(); int i=0; for(i=0; i < n/2; i++) { if(s.charAt(i)!= s.charAt(n-1-i)) break; } if(i>=n/2) return true; else return false; } public static boolean bijao(String s1,String s2){ if(s2 == null) return true; int n = Math.min(s1.length(),s2.length()); for(int i = 0; i< n;++i){ if((int)s1.charAt(i)>(int)s2.charAt(i)) return true; else return false; } if(n==s1.length()) return false; else return true; } }
Ⅵ java程序。字符串判断是否为“回文”
方法改为
static boolean isPalindrome(String str) {//判断字符串是否为“回文”
StringBuffer buf = new StringBuffer(str).reverse();
return buf.toString().equals(str);
}
Ⅶ Java中编写一个方法,判断某字符串是否为回文
/** 然后在main方法中调用这个方法打印出10000~99999之间所有的回文数 */public class TestHuiWen { static int count = 0;// 计数 public static void main(String[] args) { int g, s, q, w;// 定义个、十、千、万位 for (int i = 10000; i <= 99999; i++) { g = i % 10;//取出个位数 s = i % 100 / 10;//取出十位数 q = i % 10000 / 1000;//取出千位数 w = i / 10000;//取出万位数 if (g == w && s == q) { System.out.print(i + "%9"); count++; if (count % 4 == 0) System.out.println(); } } }}
Ⅷ java编程回文串
importstaticjava.lang.System.*;
importjava.util.Scanner;
publicclassProgram{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(in);
System.out.print("请键入字符串:");
Stringvalue=scanner.next();
booleanisSame=true;
scanner.close();
for(inti=0;i<value.length();i++){
intj=value.length()-i-1;
if(i>=j)break;
if(value.charAt(i)!=value.charAt(j)){
isSame=false;
break;
}
}
if(isSame)out.println(value+"是回文串");
elseout.println(value+"不是回文串");
}
}
Ⅸ java判断输入一段字符串是不是回文
importjava.util.Scanner;
publicclassA{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
System.out.println("请输入一个字符串:");
Stringstr=scanner.next();
booleanisloop=isLoop(str);
if(isloop){
System.out.println(str+"是回文字符");
}else{
System.out.println(str+"不是回文字符");
}
}
publicstaticbooleanisLoop(Stringstr){
for(inti=0,j=str.length()-1;i<str.length()/2;i++,j--){
if(str.charAt(i)!=str.charAt(j)){
returnfalse;
}
}
returntrue;
}
}