導航:首頁 > 編程語言 > java字元串迴文

java字元串迴文

發布時間:2022-08-12 14:59:33

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+"是迴文");}}

(3)java字元串迴文擴展閱讀:

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;
}
}

閱讀全文

與java字元串迴文相關的資料

熱點內容
dos命令建文件夾命令 瀏覽:378
解壓的密碼htm被屏蔽 瀏覽:502
冬天太冷冰箱壓縮機不啟動怎麼辦 瀏覽:83
手機打開vcf需要什麼編譯器 瀏覽:910
加密磁碟後開機很慢 瀏覽:271
長沙智能雲控系統源碼 瀏覽:256
阿里雲伺服器如何設置操作系統 瀏覽:999
超級命令的英文 瀏覽:782
做賬為什麼要用加密狗 瀏覽:586
考研群體怎麼解壓 瀏覽:158
linux修改命令提示符 瀏覽:226
圓圈裡面k圖標是什麼app 瀏覽:63
pdf加空白頁 瀏覽:947
linux伺服器如何看網卡狀態 瀏覽:318
解壓新奇特視頻 瀏覽:707
圖書信息管理系統java 瀏覽:554
各種直線命令詳解 瀏覽:864
程序員淚奔 瀏覽:147
素材怎麼上傳到伺服器 瀏覽:517
android百度離線地圖開發 瀏覽:191