? 今天上java第九课,感觉我要努力了,因为确实有的我不知道。今天讲了1.不定项参数?? 2.常量?? 3.包装类??? 4.Object类?? 5.String类?? 6.StringBuffer和StringBuilder??
?? 感受:1.会了Object类的equals()和toString()方法的重写。2.String类的方法真多3.StringBuffer和StringBuilder类很有用。在一个for循环里,循环10万次,让"abc"加"xyz"五万次,用String+="xyz"用时14375ms,用StringBuffer用时15ms,用StringBuilder用时16ms(后两个方法循环30万次),太神奇了!
?
上图
?
? 下面说下作业,判断一个邮箱地址是否符合规范。
?
1.有且只能有一个@,至少有一个“.”,
2.开头不能使@ 或“.”,结尾也一样
3.@和“.”不能在一起
?
public class CheckMail {
public boolean check(String mail){ if (mail.contains("@")&&mail.contains(".")) { if(mail==null||mail.startsWith("@")||mail.startsWith(".")||mail.endsWith("@")||mail.endsWith(".")){ return false; }else{ if(checkAT(mail)){ int i=mail.indexOf("@"); if(mail.substring(i-1, i+2).contains(".")){ return false; }else { if(checkPointCon(mail)){ return false; }else return true; } }else return false; } }else { return false; } } private boolean checkAT(String mail) { //检查是否只有一个@ // TODO Auto-generated method stub int i=mail.indexOf("@"); int j=mail.lastIndexOf("@"); if(i==j){ return true; }else{ return false; } } private boolean checkPointCon(String mail) {//检查点和点之间是否有间隔 int k=mail.indexOf("."); int j=0; for (int i = 0; i < mail.lastIndexOf("."); i++) { j=mail.indexOf(".", k+1); if(k==j-1){ return true; }else{ k=j; } } return false; } }?
还有判断一个网址是否符合规范
?
1.以http://或https://或www.开头
2.以".com",".org",".net",".cn",".edu","gov",".mil"结尾,不全啊!
3.两个"."不能直连
?
?
public class CheckWebSite { String[] ends={".com",".org",".net",".cn",".edu","gov",".mil"}; public boolean check(String web){ if (web.startsWith("http://")) { web=web.replace("http://", ""); if (web.startsWith("www.")) { for (int i = 0; i < ends.length; i++) { if (web.endsWith(ends[i])) { if(!checkPointCon(web)){ return true; }else { return false; } } } return false; }else { return false; } } else if(web.startsWith("https://")){ web=web.replace("https://", ""); if (web.startsWith("www.")) { for (int i = 0; i < ends.length; i++) { if (web.endsWith(ends[i])) { if(!checkPointCon(web)){ return true; }else { return false; } } } return false; }else { return false; } } else if(web.startsWith("www.")){ for (int i = 0; i < ends.length; i++) { if (web.endsWith(ends[i])) { if(!checkPointCon(web)){ return true; }else { return false; } } } return false; } else { return false; } } private boolean checkPointCon(String mail) {//检查点和点之间是否有间隔 int k=mail.indexOf("."); int j=0; for (int i = 0; i < mail.lastIndexOf("."); i++) { j=mail.indexOf(".", k+1); if(k==j-1){ return true; }else{ k=j; } } return false; } }
?最后说下Math的用法:
1.四舍五入 ?Math.rint(1.6);结果为2
2.求立方根 ?Math.pow(8, 1/3.0);结果为2. ? ? pow()方法表示?返回第一个参数的第二个参数次幂的值。