一.Math类
1.学会Math.E,Math.PI,Math.abs,Math.max,random()等的使用
package com.gongsi.cn.oa.work803.test1;
public class Math_ {
public static void main(String[] args) {
System.out.println(Math.E);
System.out.println(Math.PI);
double a=Math.abs(-95.99);
System.out.println(a);
int bjyx=Math.max(85, 1005);
System.out.println(bjyx);
System.out.println("生成随机数:");
for (int i = 0; i < 10; i++) {
int random=(int)(Math.random()*10);
System.out.print("\t"+random);
}
}
}
2.使用random()来进行小练习
package com.gongsi.cn.oa.work803.test2;
import java.util.Arrays;
public class DoubleColor {
public static void main(String[] args) {
int[] reds=new int[6];
//测试是否生成的数组真的没有重复的数
// for (int j = 0; j < 6; j++) {
// for (int i = 0; i < reds.length; i++) {
// int random=(int)(Math.random()*33)+1;
// //进行判断是否有重复的数
// while (isSame(reds, random)) {
// random=(int)(Math.random()*33)+1;
// }
//
// reds[i]=random;
//
// }
// System.out.println("红球生成的数为:"+Arrays.toString(reds));
// }
for (int i = 0; i < reds.length; i++) {
int random=(int)(Math.random()*33)+1;
//进行判断是否有重复的数
while (isSame(reds, random)) {//为true时,语句执行,有了重复的数,重新生成
random=(int)(Math.random()*33)+1;
}
reds[i]=random;
}
System.out.println("红球生成的数为:"+Arrays.toString(reds));
int random2=(int)(Math.random()*16)+1;
System.out.println("蓝球生成的数为:"+random2);
}
//进行判断新生成的随机数在数组中有没有重复
public static boolean isSame(int[] reds,int random) {
boolean flag=false;
for (int i = 0; i < reds.length; i++) {
if (reds[i]==random) {
flag= true;
break;
}
}
return flag;
}
}
二.length()和equals()的使用
1.练习题 对用户名和密码进行限制来完成注册功能
package com.gongsi.cn.oa.work803.test3;
import java.util.Scanner;
//注册功能
public class Register {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("请输入用户名:");
String userName=scanner.next();
if (userName.length()<3) {
System.out.println("您的输入不规范,用户名的长度不能小于3!");
return;
}
System.out.print("请输入密码:");
String password=scanner.next();
if (password.length()<6) {
System.out.println("您的输入不规范,密码的长度不能小于6!");
return;
}
System.out.print("请再次输入密码:");
String repassword=scanner.next();
// if (!repassword.equals(password)) {
// System.out.println("请输入与前一次相同的密码");
// return;
// }
//equalsIgnoreCase忽视大小写 toLowerCase()转换成小写 toUpperCase()转换成大写
//不论大小写
if (!repassword.toUpperCase().equals(password.toUpperCase())) {
System.out.println("请输入与前一次相同的密码");
return;
}
System.out.println("注册成功");
}
}
三.==和equals的区别(从内存分析入手)
1.代码运行直观体现
package com.gongsi.cn.oa.work803.test3;
// ==和equals的区别比较
public class TestEquals {
public static void main(String[] args) {
String a="博君一肖";
String b="博君一肖";
System.out.println(a==b);//放在常量池 用同一个常量,地址相同 true
System.out.println(a.equals(b));//内容相同 true
String c=new String("博君一肖");
String d=new String("博君一肖");
System.out.println(c==d);//地址不相同,new出来的放在堆里,c和d指向两个不同的堆 false
System.out.println(c.equals(d));//内容相同 true
System.out.println(a==c);//地址不相同 false 一个在常量池里,一个在堆里
System.out.println(a.equals(c));//内容相同 true 在String.class 里面的equals方法被重写了,按照人类的主观意识来重写的
Stu e=new Stu("博君一肖");
Stu f=new Stu("博君一肖");
System.out.println(e==f);//地址不相同,new出来的放在堆里,e和f指向两个不同的堆 false
System.out.println(e.equals(f));//没重写的时候,比较的还是地址 在Object.class
}
}
class Stu{
private String name;
public Stu(String name) {
super();
this.name = name;
}
}
2.从内存分析看区别
四.练习 判断字符在字符串中出现的次数
package com.gongsi.cn.oa.work803.test3;
import java.util.Scanner;
//查找某字符在某字符串中有多少个
public class TestCheck {
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
System.out.print("请输入字符串:");
String str1=input.next();
System.out.print("请输入您要查找的字符:");
String str2=input.next();
// String[] str=str1.split("");
String[] str=new String[str1.length()];
for (int i = 0; i < str.length; i++) {
// 把字符串放入数组 substring(i, i+1):一个一个的放
String str3=str1.substring(i, i+1);
str[i]=str3;
}
int count=0;
for (int i = 0; i < str.length; i++) {
if (str[i].equals(str2)) {
count++;
}
}
System.out.println("\""+str1+"\""+"中包含"+count+"个"+"\""+str2+"\"");
}
}
四. split
1.练习题:歌词分割
package com.gongsi.cn.oa.work803.test3;
//歌词每句按行输出
public class TestGeCi {
public static void main(String[] args) {
//------原歌词模式------
String song="长亭外 古道边 芳草碧连天 晚风拂 柳笛声残 夕阳山外山";
String[] song1=song.split(" ");
//------拆分后歌词模式------
for (String string : song1) {
System.out.println(string);
}
}
}
2.练习
package com.gongsi.cn.oa.work803.test3;
import java.util.Arrays;
public class TestSplit {
public static void main(String[] args) {
// String str="sjb sjb sjb";
// String [] strs=str.split(" ");
// System.out.println(Arrays.toString(strs));
String str="sjb|sjb|sjb";
String [] strs=str.split("\\|");//以“|”分割,要转义,转义字符还要再转一次
System.out.println(Arrays.toString(strs));
}
}
五.String和StringBuffer的内存区别
package com.gongsi.cn.oa.work803.test3;
//StringBuffer与string比较内存空间 StringBuild
public class TestStringBuffer {
public static void main(String[] args) {
String a="zgxxy";
String b=a+"d"+"d"+"a"+"n";
System.out.println(b);
// StringBuffer stringBuilder=new StringBuffer("bjyxszd");
// System.out.println(stringBuffer);
// stringBuffer=stringBuffer.append("z").append("g").append("d").append("d").append("a").append("n");
// System.out.println(stringBuffer);
//
StringBuilder stringBuilder1=new StringBuilder("bjyxszd");
System.out.println(stringBuilder1);
stringBuilder1=stringBuilder1.append("z").append("g").append("d").append("d").append("a").append("n");
System.out.println(stringBuilder1);
}
}
六.一些常用
package com.gongsi.cn.oa.work803.test3;
public class TestString {
public static void main(String[] args) {
String str=" zdddan bjyxszd";
//indexOf("a") a最开始出现在此字符串的位置
int r=str.indexOf("a");
System.out.println(r);
//lastindexOf("d") d最后出现在此字符串的位置
int r2=str.lastIndexOf("d");
System.out.println(r2);
//substring(6) 从第六位开始,返回一个新的字符串
String r3=str.substring(6);
System.out.println(r3);
//substring(6,13) ,返回一个从第六位到第12位的新的字符串
String r4=str.substring(6, 13);
System.out.println(r4);
//返回的字符串去掉前后空格,中间的空格保留
String r5=str.trim();
System.out.println(r5);
}
}