韩顺平Java30——常用类Homework
1.
public static void main(String[] args) { String s="abcdef"; System.out.println("交换前:"+s); try { s=reverse(s,1,4); } catch (Exception e) { System.out.println(e.getMessage()); return; } System.out.println("交换后:"+s); } public static String reverse(String str,int start,int end){ char [] chars = str.toCharArray(); char temp = ' '; if (!(str!=null && start < end && start>=0 && end<str.length())){ throw new RuntimeException("参数错误!"); } for (int i =start,j = end;i < j;i++,j--){ temp=chars[i]; chars[i]=chars[j]; chars[j]=temp; } return new String(chars); }
2.
package homework; import java.util.Scanner; /** * @author 紫英 * @version 1.0 * @discription */ public class Homework05 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入id:"); String id = scanner.next(); System.out.println("请输入password:"); String password = scanner.next(); System.out.println("请输入email:"); String email = scanner.next(); try { userRig(id, password, email); System.out.println("success!"); } catch (Exception e) { System.out.println(e.getMessage()); } } public static void userRig(String id, String password, String email) { if (!(email != null && password != null && email!=null)){ throw new RuntimeException("参数不能为空"); } if (!(id.length() >= 2 && id.length() <= 4)) { throw new RuntimeException("用户名长度需为2-4个字符!"); } if (!((password.length() == 6) && isNum(password))) { throw new RuntimeException("密码格式有误"); } if (isEmail(email) == false) { throw new RuntimeException("email格式有误"); } } public static boolean isNum(String password) { // 检测是否为数字 char[] p = password.toCharArray(); for (int i = 0; i < p.length; i++) { // 这个我没想到 if (p[i] < '0' || p[i] > '9') { return false; } } return true; } public static boolean isEmail(String email) { // 检测@是否在.前面 StringBuffer sb = new StringBuffer(email); if (!(sb.indexOf("@") - sb.indexOf(".") < 0)) { return false; } return true; } }
3.
package homework; import java.util.Locale; import java.util.Scanner; /** * @author 紫英 * @version 1.0 * @discription 名字格式化 */ public class Homework07 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入名字:"); String name = scanner.nextLine(); nameformat(name); } public static void nameformat(String name) { if (name==null){ System.out.println("名字为空!"); return; } String[] sname = name.split(" "); if (sname.length!=3){ System.out.println("名字格式不正确!(Xxx xxx Xxx)"); return; } System.out.println(sname[2] + "," + sname[0] + "," + " ." + sname[1].toUpperCase(Locale.ROOT).charAt(0)); } }
老师改进:
System.out.println(String.format("%s,%s .%c",sname[2] ,sname[0],sname[1].toUpperCase(Locale.ROOT).charAt(0)));
4.
package homework; import java.util.Scanner; /** * @author 紫英 * @version 1.0 * @discription */ public class Homework08 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入字符:"); String s=scanner.next(); judge(s); } public static void judge(String s){ int up=0; int down=0; int num=0; int other=0; char[] c=s.toCharArray(); for (char c1 : c) { if (c1>='a'&&c1<='z'){ down++; } if (c1>='A'&&c1<='Z'){ up++; } if (c1>='0'&&c1<='9'){ num++; }else { other++; } } System.out.println("大写字母"+up+"个"+"小写字母"+down+"个"+"数字"+num+"个"+"其他字符"+other+"个"); } }
5.
FFTFFT
本文来自博客园,作者:紫英626,转载请注明原文链接:https://www.cnblogs.com/recorderM/p/15743316.html