1-API介绍
- 介绍 : API 应用程序编程接口
- 简单记 : 就是别人已经写好的代码
- 学习别人已经写好的代码, 使用的是 API 帮助文档
- 帮助文档的使用流程 :
API帮助文档的使用流程 :
1. 在索引的位置搜索自己要查看的类
- 思考: 我怎么知道, 自己要看哪一个类?
- 回答: 开发中常用的类, 在课程中都会讲解到
2. 看该类所在的包
- 目标: 看这个包是不是 java.lang (核心包)
是 : 使用的时候不需要导包
不是 : 使用的时候必须要编写导包代码
3. 看该类的介绍
- 目标: 明确出这个类的作用
4. 看该类的构造方法
- 目标: 能够根据构造方法, 创建出该类的对象
5. 看该类的成员方法
- 1. 方法名
- 2. 方法的参数
- 3. 方法的返回值
- 4. 方法的介绍
2-String
String 概述
String 类在 java.lang 包下,所以使用的时候不需要导包
String 类代表字符串,Java 程序中的所有字符串文字(例如“abc”)都被实现为此类的实例
也就是说,Java 程序中所有的双引号字符串,都是 String 类的对象
字符串不可变,它们的值在创建后不能被更改
字符串的比较
使用 == 做比较
基本类型:比较的是数据值是否相同
引用类型:比较的是地址值是否相同
字符串是对象,它比较内容是否相同,是通过一个方法来实现的,这个方法叫:equals()
public boolean equals(Object anObject):将此字符串与指定对象进行比较。由于我们比较的是字符串对象,所以参数直接传递一个字符串
案例 :用户登录
package com.wanxi.test;
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
String usrname = "aaa";
String pasword1 = "123456";
Gatunmber gatunmber=new Gatunmber();
/* for (int i = 0; i < 3; i++) {
String usname=gatunmber.aa("用户名:");
String password=gatunmber.aa("密码:");
judae(usrname,usname,pasword1,password);
}*/
int temp=0;
while (true){
System.out.println("一共"+3+"机会还有"+(3-temp)+"次机会");
String usname=gatunmber.aa("用户名:");
String password=gatunmber.aa("密码:");
if (judae(usrname,usname,pasword1,password)) {
return;
}else if (temp==2){
return;
}
temp++;
}
}
private static boolean judae(String usrname,String usname,String pasword1, String password) {
if (usrname.equals(usname) && pasword1.equals(password)) {
System.out.println("登陆成功");
return true;
}else {
System.out.println("用户名或密码错误!");
return false;
}
}
}
案例 :遍历字符串
package com.wanxi.test;
/*
* 思路:
键盘录入一个字符串,用 Scanner 实现
将字符串拆分为字符数组
public char[] toCharArray( ):将当前字符串拆分为字符数组并返回
遍历字符数组
* */
public class TraversalString {
//需求:键盘录入一个字符串,使用程序实现在控制台遍历该字符串
public static void main(String[] args) {
//首先键盘录入
Gatunmber gatunmber = new Gatunmber();
String str = gatunmber.aa("请输入你需要遍历的字符串:");
/* Scanner scanner = new Scanner(System.in);
System.out.println("请输入字符串:");
String str=scanner.next();*/
char[] arr = str.toCharArray();//将当前字符串拆分为字符数组
for (int i = 0; i < arr.length; i++) {//遍历字符数组
System.out.print(arr[i]);
}
}
}
案例 :统计字符次数
package com.wanxi.test;
/*
需求:键盘录入一个字符串,统计该字符串中大写字母字符,小写字母字符,数字字符出现的次数(不考虑其他字符)
思路:
键盘录入一个字符串,用 Scanner 实现
要统计三种类型的字符个数,需定义三个统计变量,初始值都为0
遍历字符串,得到每一个字符
判断该字符属于哪种类型,然后对应类型的统计变量+1
假如ch是一个字符,我要判断它属于大写字母,小写字母,还是数字,直接判断该字符是否在对应的范围即可
大写字母:ch>='A' && ch<='Z'
小写字母: ch>='a' && ch<='z'
数字: ch>='0' && ch<='9'
输出三种类型的字符个数
*/
public class CountTheNumber {
public static void main(String[] args) {
Gatunmber gatunmber = new Gatunmber();
String str = gatunmber.aa("请输入你的字符串");
/*char[] arr = str.toCharArray();*/
int da = 0;
int xiao = 0;
int num = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= 'A' && str.charAt(i)<='Z') {
da++;
}
if (str.charAt(i) >= 'a' && str.charAt(i)<='z') {
xiao++;
}
if (str.charAt(i) >= '0' && str.charAt(i)<='9') {
num++;
}
}
/* for (int i = 0; i < arr.length; i++) {
int unmber = str.charAt(i);
if (unmber >= 65 && unmber <= 90) {
da++;
}
if (unmber >= 97 && unmber <= 122) {
xiao++;
}
if (unmber >= 49 && unmber <= 57) {
num++;
}
}*/
System.out.println(" 大写:" + da + " 小写:" + xiao + " 数值:" + num);
}
}
案例 :手机号屏蔽
package com.wanxi.test;
/*需求:以字符串的形式从键盘接受一个手机号,将中间四位号码屏蔽
最终效果为:156****1234
思路:
键盘录入一个字符串,用 Scanner 实现
截取字符串前三位
String substring(int beginIndex, int endIndex) :从beginIndex索引位置开始截取,截取到endIndex索引位置,得到新字符串并返回(包含头,不包含尾)
String s1 = telString.substring(0,3);
截取字符串后四位
String substring(int beginIndex) : 从传入的索引位置处,向后截取,一直截取到末尾,得到新的字符串并返回
String s2 = telString.substring(7);
将截取后的两个字符串,中间加上****进行拼接,输出结果
*/
public class AntianaPhylaxis {
public static void main(String[] args) {
Gatunmber gatunmber = new Gatunmber();
String str = gatunmber.aa("请输入你的电话号码:");
for (int i = 0; i < str.length(); i++) {
if (i<3){
System.out.print(str.charAt(i));
}else if (i>6){
System.out.print(str.charAt(i));
}else {
System.out.print("*");
}
}
}
}
案例 :敏感词替换
package com.wanxi.test;
/*需求:键盘录入一个 字符串,如果字符串中包含(TMD),则使用***替换
思路:
键盘录入一个字符串,用 Scanner 实现
替换敏感词
String replace(CharSequence target, CharSequence replacement)
将当前字符串中的target (被替换的旧值 )内容,使用replacement (替换的新值) 进行替换
返回新的字符串
输出结果*/
public class SensitiveWordReplacement {
public static void main(String[] args) {
Gatunmber gatunmber = new Gatunmber();
String str = gatunmber.aa("请输入你的字符;");
String tempstr = str.replaceAll("TMD", "***");
System.out.println(tempstr);
}
}
案例 :切割字符串
package com.wanxi.test;
import java.util.Scanner;
/*需求:以字符串的形式从键盘录入学生信息,例如:“张三 , 23”从该字符串中切割出有效数据
封装为Student学生对象
思路:
编写Student类,用于封装数据
键盘录入一个字符串,用 Scanner 实现
根据逗号切割字符串,得到(张三)(23)
String[] split(String regex) :根据传入的字符串作为规则进行切割,将切割后的内容存入字符串数组中,并将字符串数组返回
从得到的字符串数组中取出元素内容,通过Student类的有参构造方法封装为对象
调用对象getXxx方法,取出数据并打印。*/
public class CutString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名年龄:");
String str = scanner.next();
String[] split = str.split(",");
// for (int i = 0; i < split.length; i++) {
// System.out.println(split[i]);
// }// Integer age=Integer.valueOf(split[1]);
Student student=new Student(split[0],split[1]);
System.out.println(student.getAge()+student.getName());
}
}