各自努力,最高处见!加油!
一、指定字符串翻转
package String_Learn;
public class HomeWork {
public static void main(String[] args) {
String str = "abcderfge";
System.out.println(str);
try {
str = reverse(str, 2, 100);
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 实现start到end位置字符串的位置翻转
*/
public static String reverse(String str, int start, int end) {
//对输入的参数进行验证
if (!(str != null && start < end && end <= str.length())) {
throw new RuntimeException("参数输入异常!");
}
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;
}
return new String(chars);//错误点:应该返回一个String对象,而不是一个String变量。
}
}
对输入参数进行验证:(技巧)
- 写出正确的情况
- 取反
二、注册处理题
import java.util.Scanner;
/**
* 输入用户名、密码、邮箱,如果信息录入正确,则提示注册成功,否则生成异常对象
* 要求:
* 1.用户名长度为2或3或4
* 2.密码的长度为6,要求全是数字
* 3.邮箱中包含@和. 并且@在.前面
*/
public class HomeWork2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入用户名:");
String userName = sc.next();
System.out.print("请输入密码:");
String userPassword_Str = sc.next();
System.out.print("请输入邮箱:");
String userMail = sc.next();
checkIn(userName,userPassword_Str,userMail);
System.out.println("恭喜你注册成功!");
}
public static void checkIn(String userName,String userPassword_Str,String userMail) {
if (!(userName.length() == 2 || userName.length() == 3 || userName.length() == 4)) {
throw new RuntimeException("用户名的长度只能为2或3或4");
}
if (!(userPassword_Str.length() == 6)) {
throw new RuntimeException("密码长度应为6!");
}
if(isDigital(userPassword_Str)){
throw new RuntimeException("密码只能为数字");
}
int i=userMail.indexOf('@');
int j=userMail.indexOf('.');
if(!(i>0&&i<j)){
throw new RuntimeException("邮箱格式不规范");
}
//自己写的代码,没有合理利用String的方法
// char[] mailChars = userMail.toCharArray();
// int rem1=userMail.length();//记录@符号的位置
// int rem2=userMail.length();//记录.符号的位置
// for (int i = 0; i < userMail.length(); i++) {
//
// if (mailChars[i] == '@') {
// rem1 = i;
// }
// if (mailChars[i] == '.') {
// rem2 = i;
// }
// }
// if (!(rem1 < rem2 && rem1 > 0 && rem2 > 0 && rem1 < userMail.length() && rem2 < userMail.length())) {
// throw new RuntimeException("缺少“@”或“.”!");
// }
}
public static boolean isDigital(String psw){
char[] pswChar = psw.toCharArray();
for (int i = 0; i < psw.length(); i++) {
if(pswChar[i]<'0'||pswChar[i]>'9'){
return true;//不符合要求返回true
}
}
return false;
}
}
三、String内存布局测试题
public class Homework05 {
public static void main(String[] args) {
class Animal{
String name;
public Animal(String name){
this.name=name;
}
}
String s1="hspedu";
Animal a=new Animal(s1);
Animal b=new Animal(s1);
System.out.println(a==b);//F这里比较的是a对象与b对象的地址,不是相同的对象自然也不相等。
System.out.println(a.equals(b));//F a和b不是相同对象
System.out.println(a.name==b.name);//T
String s4=new String("hspedu");
String s5="hspedu";
System.out.println(s1==s4);//F s1指向常量池,s4指向堆中的value
System.out.println(s4==s5);//F s5指向常量池,s4指向堆中的value
String t1="hello"+s1;//常量相加看池,变量相加在堆中
String t2="hellohspedu";
System.out.println(t1.intern()==t2);// 当调用intern方法时,如果池已经包含与equals(Object)方法确定的相当于此String对象的字符串,
// 则返回来自池的字符串。 否则,此String对象将添加到池中,并返回对此String对象的引用。
}
}