习题:
输入用户名,密码,邮箱,如果信息录入不正确则提示注册成功,否则生成异常对象。
- 用户名长度为2或者3或者4。
- 密码长度为6,要求全是数字。
- 邮箱中包含@和.并且@在.的前面。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入用户名:");
String next = null;
try {
next = scanner.next();
if(!(next.length()==2||next.length()==3||next.length()==4)){
throw new RuntimeException("用户名长度不符合要求。");
}
} catch (RuntimeException e) {
System.out.println("用户名是:"+next+",不符合要求。");
}
System.out.println("输入密码:");
String next1 = null;
try {
next1 = scanner.next();
int i = Integer.parseInt(next1);
if(!(next1.length()==6)){
throw new RuntimeException("密码长度不符合。");
}
} catch (RuntimeException e) {
System.out.println("密码是:"+next1+"不符合要求。");
}
System.out.println("输入邮箱:");
try {
String next2 = scanner.next();
int i = next2.indexOf("@");
int i1 = next2.indexOf(".");
if(!(i>0 && i1>0 && i<i1)){
throw new RuntimeException("邮箱格式不对。");
}
} catch (RuntimeException e) {
System.out.println("邮箱格式不正确。");
}
scanner.close();
public static void main(String[] args) {
String name = "张瑊";
String pwd = "null";
String email = "[email protected]";
try {
usreRegister(name, pwd, email);
System.out.println("用户注册成功。");
} catch (Exception e) {
System.out.println("出现异常:" + e.getMessage());
}
}
public static void usreRegister(String name, String pwd, String email) {
if (!(name.length() == 2 || name.length() == 3 || name.length() == 4)) {
throw new RuntimeException("用户的名字不符合要求。");
}
if (!(pwd.length() == 6 && digital(pwd))) {
throw new RuntimeException("密码格式不对。");
}
int i = email.indexOf("@");
int i1 = email.indexOf(".");
if (!(i > 0 && i1 > i)) {
throw new RuntimeException("邮箱格式不对。");
}
编写Java程序,输入形式为:Han Shun Ping的人名,以Ping,Han.S的形式打印出来。
public class Homework05 {
public static void main(String[] args) {
println("null");
}
public static void println(String s){
if(s==null){
System.out.println("长度不能为空。");
return;
}
String[] s1 = s.split(" ");
if(s1.length!=3){
System.out.println("长度要为3。你的字符串长度为:"+s1.length);
return;
}
String format = String.format("%s,%s.%s ", s1[2], s1[0], s1[1].toUpperCase(Locale.ROOT).charAt(0));
System.out.println(format);
}
}
输入字符串,判断里面有多少个大写字母,多少个小写字母,多少个数字。
public static void main(String[] args) {
printName("11234124zjcjAAAAf");
}
public static void printName(String s){
if(s==null){
System.out.println("s不能为空。");
return;
}
char[] chars = s.toCharArray();
int numberCount=0;
int lowerCount=0;
int upperCount=0;
int otherCount=0;
for (int i = 0; i <chars.length; i++) {
if(chars[i]>'0'&& chars[i]<'9'){
numberCount++;
}else if(chars[i]>'a'&&chars[i]<'z'){
lowerCount++;
}else if(chars[i]>'A'&&chars[i]<'Z'){
upperCount++;
}
else{
otherCount++;
}
}
System.out.println("数字有:"+numberCount);
System.out.println("小写字母有:"+lowerCount);
System.out.println("大写字母有:"+upperCount);
System.out.println("其他字符有:"+otherCount);
}
将字符串指定部分进行反转,比如将"abcdef"反转成"aedcbf"
public static void main(String[] args) {
String s = "abcdef";
System.out.println("原字符串是:"+s);
try {
System.out.println("现字符串是:"+reverse(s, 1, 39));
} catch (Exception e) {
System.out.println("参数不正确.");
}
}
public static String reverse(String s, int start, int end) {
if(!(s!=null&&start>=0&&start<end&&end>start&&end<s.length())){
throw new ArrayIndexOutOfBoundsException("参数不正确。");
}
char[] chars = s.toCharArray();
char temp;
for (int i = start, j = end; i < j; i++, j--) {
temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
return new String(chars);
}z