题目1:编写一个应用程序,统计输入的一个字符串中相同字符的个数,并将统计结果输出。
一、代码
package gy1; import java.util.*; public class Test1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入字符串:"); String str= in.next(); char a,b,c; int count = 1; for (int i = 0; i < str.length(); i++) { a = str.charAt(i); boolean boo = false;// 判断是否已经输出过a这个值的数量 for (int j = i; j >= 0; j--) { if (j != 0) { c = str.charAt(j - 1); if (a == c) { count = 1; boo = true; break; } } } // 如果已经输出过则continue; if (boo) { continue; } // 循环查询这个值的数量 for (int j = i + 1; j < str.length(); j++) { b = str.charAt(j); if (a == b) { count++; } } System.out.println(a + "的数量为:" + count); count = 1; } } }
二、运行结果

题目2:编写程序,输入一个字符串,判断该串中的字母能否组成一个回文串(回文串:一个字符串从前向后读取和从后向前读取都一样)。如:ab<c>c?ba
一、代码
package work; import java.util.*; public class Check { public static void main(String[] args) { Scanner in=new Scanner(System.in); System.out.println("请输入字符串:"); String str=in.nextLine(); StringBuffer s=new StringBuffer(str); s.reverse(); //倒置字符 String str1=s.toString();//转换为String类型 if(str.equals(str1)){ System.out.println(str+"是回文"); }else{ System.out.println(str+"不是回文"); } } }
二、运行结果


浙公网安备 33010602011771号