public static void main(String[] args) {
String s = "abcdef";
System.out.println("交换前================");
System.out.println(s);
System.out.println("交换后=================");
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) {
if(!(str!=null && start<end && start >= 0 && end <str.length())){
throw new RuntimeException("参数不正确");
}
char[] sArr = str.toCharArray();
char temp = ' ';
for (int i = start,j= end; i < j; i++,j--) {
temp = sArr[i];
sArr[i] = sArr[j];
sArr[j] = temp;
}
return new String(sArr);
}
交换前================
abcdef
交换后=================
aedcbf
交换前================
abcdef
交换后=================
参数不正确
public class Journey {
public static void main(String[] args) {
String username = "kere";
String passward = "123456";
String email = "123@qq.com";
try {
userRegister(username,passward,email);
System.out.println("恭喜你 注册成功!!!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void userRegister(String name,String pwd,String email){
if(!(name!=null&&pwd!=null&&email!=null)){
throw new RuntimeException("参数不能为空哟!!!");
}
int nameLength = name.length();
if(!(nameLength>=2&&nameLength<5)){
throw new RuntimeException("用户名长度不正确!!!");
}
if(!(pwd.length()==6&&digital(pwd))){
throw new RuntimeException("密码长度不正确或密码中包含有非法字符");
}
int index1 = email.indexOf("@");
int index2 = email.indexOf(".");
if(!(index1>-1 && index1<index2)){
throw new RuntimeException("邮箱格式错误");
}
}
public static boolean digital(String pwd){
char[] pwdArr = pwd.toCharArray();
for (int i = 0; i < pwdArr.length; i++) {
if(!(pwdArr[i]<'9'&&pwdArr[i]>'0')){
return false;
}
}
return true;
}
}
public class Journey {
public static void main(String[] args) {
String name = "Dss kerwin Joe";
printName(name);
}
public static void printName(String str) {
if (str == null) {
System.out.println("str 不能为空哟");
return;
}
String[] s = str.split(" ");
if (s.length != 3) {
System.out.println("输入的名字格式不正确!!!");
return;
}
String formatRes = String.format("%s, %s .%c", s[2], s[0], s[1].toUpperCase().charAt(0));
System.out.println(formatRes);
}
}
public class Journey {
public static void main(String[] args) {
String youth = "ABjso340xW";
countStr(youth);
System.out.println("=============");
String ess = "csijdcbsljdbv23bux899AAA";
countStr(ess);
System.out.println("=======================");
String ker = null;
try {
countStr(ker);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void countStr(String str){
if(str==null){
throw new RuntimeException("给定的参数不能为空哟!!!");
}
char[] youthAAA = str.toCharArray();
int NumCount = 0;
int UpperCount = 0;
int LowerCount = 0;
for(char i : youthAAA){
if(i<='9'&&i>='0'){
NumCount++;
}else if(i<='z'&&i>='a'){
LowerCount++;
}else if(i<='Z'&&i>='A'){
UpperCount++;
}
}
System.out.println("字符串中有"+NumCount+"个数字");
System.out.println("字符串中有"+UpperCount+"个大写字母");
System.out.println("字符串中有"+LowerCount+"个小写字母");
}
}
字符串中有3个数字
字符串中有3个大写字母
字符串中有4个小写字母
=============
字符串中有5个数字
字符串中有3个大写字母
字符串中有16个小写字母
=======================
给定的参数不能为空哟!!!