以下题目都用方法实现,再在主方法中进行测试
1、给定一个数组nums = {‘A’,‘B’, ‘C’},要求以[A,B,C]的值输出
import java.util.Arrays; public class Diyi { public static void main(String[] args) { xiazhi121(); } public static void xiazhi121() { char[] nums = { 'A', 'B', 'C' }; char[] array= nums; String sum = Arrays.toString(array);//转换成String类型 System.out.println(sum); } }
2、将一字符串反转后,再输出。如“ase234”->“432esa”
import java.util.Scanner; public class Dier { public static void main(String[] args) { xiazhi121(); } public static void xiazhi121() { Scanner sc = new Scanner(System.in); System.out.println("请输入需要反转的字符串:"); String line = sc.nextLine(); StringBuffer fanzhuang = new StringBuffer(line); fanzhuang = fanzhuang.reverse();//字符串反序输出 System.out.println("反转之后的字符串为:" +"\n" + fanzhuang); } }
3、统计一个字符串在另一个字符串中出现的次数
public class Disan { public static void main(String[] args) { xiazhi121(); } public static void xiazhi121(){ String s = "ddffffbbbdddbbbfdsa"; System.out.println("第一个字符串为:"+s); String s2="bbb"; System.out.println("第二个字符串为:"+s2); String s1 = s.replace("bbb", ""); int count = (s.length() -s1.length())/s2.length();//统计次数 System.out.println("第二个字符串在第一个字符串出现的次数:"+count+"次"); } }
4、编写一方法deleteCharAt(),功能为删除字符串的一个字符
public class Dishi { public static void main(String args[]) { deldteCharAt(); } public static void deldteCharAt() { String str = "abcdDaCBA123"; System.out.println("删除前的字符串为:" + str); String strNew = str.replace("a", "");//删除字符 System.out.println("删除a字符之后:" + strNew); } }
5、输入一网站名,然后按小数点进行分割输出。如www.baidu.com输出为www baidu com
import java.util.Scanner; public class Diwu { public static void main(String[] args) { xiazhi121(); } public static void xiazhi121() { Scanner sc = new Scanner(System.in); System.out.println("请输入一个网站名:"); String line = sc.nextLine(); String[] firstArray = line.split("\\.");//按照“.”分割 for (String a : firstArray) { System.out.print(a + " ");//按要求输出 } } }
6、将一字符串中的所有空格去掉再输出
public class Diliu { public static void main(String[] args) { xiazhi121(); } public static void xiazhi121() { String str = " abc "; System.out.println("去除空格前:" + str); String quchu = str.trim();//忽略字符串首尾处空白 System.out.println("去除空格后:" + quchu); } }
7、输入一人的身份证号,判断其合法性,再输出该人的所在省名,出生年月日,性别信息
import java.util.Scanner; public class Diqi { public static void main(String[] args) { xiazhi121(); } public static void xiazhi121() { Scanner sc = new Scanner(System.in); System.out.println("请输入一个身份证号:"); String s = sc.nextLine(); int i = 18; if (i != s.length()) { System.out.println("输入身份证号码有误");//多活动少提示输入错误 } else { String year = s.substring(6, 10);//取身份证的年份 String month = s.substring(10, 12);//取身份证的月份 String day = s.substring(12, 14);//取身份证的天 System.out.println("出生年月日为:" + year + "年" + month + "月" + day + "日"); String sex = s.substring(16, 17);//取身份证代表性别的位数 int a = Integer.valueOf(sex); if (a % 2 == 0) { System.out.println("此身份证号码的人是女性"); } else { System.out.println("此身份证号码的人是男性"); } String[] sun = { "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65", "71", "81", "82" };//身份证对应的身份证第一位和第二位 String[] sum = { "北京市", "天津市", "河北省", "山西省", "内蒙古自治区", "辽宁省", "吉林省", "黑龙江省", "上海市", " 江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省", " 河南省", "湖北省", " 湖南省", "广东省", " 广西壮族自治区", "海南省", "重庆市", "四川省", "贵州省", "云南省", " 西藏自治区", "陕西省", "甘肃省", "青海省", "宁夏回族自治区", "新疆维吾尔自治区", "台湾省", "香港特别行政区", "澳门特别行政区" }; String pos = (s.substring(0, 2));//取出身份证一二位 int sheng; for ( sheng = 0; sheng < sun.length; sheng++) { if (pos.equals(sun[sheng])) { break;//找到对应的省份退出循环 } } System.out.println("此身份证的省份为:" + sum[sheng]); } } }
8、输入一字符串,分别统计出其中英文字母、空格、数字和其它字符的个数
import java.util.Scanner; public class Diba { public static void main(String[] args) { xiazhi121(); } public static void xiazhi121() { System.out.print("请输入一串字符:"); Scanner scan = new Scanner(System.in); String str = scan.nextLine();//将一行字符转化为字符串 char[] b = str.toCharArray();//将字符串转换为字符数组 int letters = 0; int num = 0; int blank = 0; int others = 0; int i = 0; while (i < b.length) { if ((b[i] >= 'A') && (b[i]) <= 'Z') { letters++;//计算英文字符个数 } else if ((b[i] >= 'a') && (b[i]) <= 'z') { letters++;//计算英文字符个数 } else if ((b[i] >= '0') && (b[i]) <= '9') { num++;//计算数字个数 } else if (b[i] == ' ') { blank++;//计算空格个数 } else { others++;//计算其他字符个数 } i++; } System.out.println("英文字符共有:" + letters + "个"); System.out.println("数字字符共有:" + num + "个"); System.out.println("空格字符共有:" + blank + "个"); System.out.println("其它字符共有:" + others + "个"); } }