练习题一
1.将字符串中指定部分进行反转,如“abcdef”转成“aedcbf”
2.输入用户名、密码、邮箱、如果信息录入正确,则提示注册成功,否则生成异常对象
3.输入形式为:Han Shun Ping的人名,以Ping,Han·s的形式打印
4.输入字符,判里面有多少个大写字母,多少个小写字母,多少个数字
1.将字符串中指定部分进行反转,如“abcdef”转成“aedcbf”
public static void main(String[] args) { //将字符串中指定部分进行反转,如“abcdef”转成“aedcbf” String str = "abcdef"; System.out.println("交换前 = " + str); try { str = reverse(str,1,4); }catch (Exception e){ System.out.println(e.getMessage()); return; } System.out.println("交换后 = " + str); } public static String reverse(String str ,int start,int end){ //校验 if(!(str != null && start >= 0 && end < str.length() && start < end)){ throw new RuntimeException("参数不正确"); } //将String字符串转换成字符数组 char[] chars = str.toCharArray(); char temp = ' ';//交换辅助变量 for(int i = start ,j = end; i < j ;i++,j--){ temp = chars[i]; chars[i] = chars[j]; chars[j] = temp; } //使用chars 重新构建一个String 返回即可 return new String(chars); }
2.输入用户名、密码、邮箱、如果信息录入正确,则提示注册成功,否则生成异常对象
* .要求: * (1)用户名长度为2或3或4 * (2)密码的长度为 6 ,要求全为数字 * (3)邮箱包含@和. ,并且 @ 在 . 的前面
public static void main(String[] args) { /* 题目: * 1. 输入用户名、密码、邮箱、如果信息录入正确,则提示注册成功,否则生成异常对象 * 2.要求: * (1)用户名长度为2或3或4 * (2)密码的长度为 6 ,要求全为数字 * (3)邮箱包含@和. ,并且 @ 在 . 的前面 * */ try{ rigister("jhan","132456","56456@qq.com"); }catch(Exception e){ System.out.println(e.getMessage()); } } public static void rigister(String name ,String pwd,String email){ // int length = name.length(); //判断name 如果不在[2,4]范围内,则报出异常 if(!(length >= 2 && length <=4)){ throw new RuntimeException("用户名的长度未在2到4的范围内"); } int length1 = pwd.length(); //判断密码 若密码的长度不为 6 ,并且不全为数字 则报出异常 if(!(length1 == 6 && number(pwd))){ throw new RuntimeException("密码的长度不为 6 或者不全为数字"); } //查找符号的索引 int i1 = email.indexOf("@"); int i2 = email.indexOf("."); if (!(i1 > 0 && i2 > i1)){ throw new RuntimeException("邮箱未包含@和. ,并且 未@ 在 . 的前面"); } System.out.println("恭喜您 ~ 注册成功..."); } public static boolean number(String pwd){ char[] chars = pwd.toCharArray(); for (int i = 0; i < chars.length; i++) { if (chars[i] < '0' || chars[i] > '9'){ return false; } } return true; }
3.输入形式为:Han Shun Ping的人名,以Ping,Han·s的形式打印
出来。其中·S是中间单词的首字母;
public static void main(String[] args) { /*1.输入形式为:Han Shun Ping的人名,以Ping,Han·s的形式打印 出来。其中·S是中间单词的首字母。 2.例如输入"Willian Jefferson Clinton"输出形式为:Clinton,Willian */ String name = "jan han bin"; pritNUM(name); } public static void pritNUM(String name){ //对字符串进行判断 if(name == null){ System.out.println("用户名不能为空~"); return; } //对字符串进行分割 String[] str = name.split(" "); if(str.length != 3 ){ System.out.println("格式不正确,请输入正确的格式 XXX XXX XXX"); return; } //将字符串格式化输出 String format = String.format("%s,%s.%c",str[2],str[0],str[1].toUpperCase().charAt(0)); System.out.println(format); }
4.输入字符,判里面有多少个大写字母,多少个小写字母,多少个数字
public static void main(String[] args) { /*输入字符,判里面有多少个大写字母,多少个小写字母,多少个数字*/ lookup("han235SS "); } public static void lookup(String str){ if(str == null){ System.out.println("字符串不能为空~"); return; } char[] chars = str.toCharArray(); int a = 0,b = 0,c = 0,d = 0; for (int i = 0; i < str.length(); i++) { if(chars[i] >= 'a' && chars[i] <= 'z'){ a++; }else if(chars[i] >= 'A' && chars[i] <= 'z'){ b++; }else if(chars[i] >= '0' && chars[i] <= '9'){ c++; }else { d++; } } System.out.println("大写字母个数为:" + b); System.out.println("小写字母个数为:" + a); System.out.println("数字个数为:" + c); System.out.println("其他字符为:" + d); }