API帮助文档
主要熟悉java.lang包,该包不需要进行 导包
练习:用户登录
package day8;
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String rightUsername=new String("2020212318");
String rightPassword=new String("ligen1209");
for (int i = 0; i <3 ; i++) {
System.out.println("请输入用户名");
String username=sc.next();
System.out.println("请输入密码");
String passWord=sc.next();
if(rightUsername.equals(username)&&rightPassword.equals(passWord)){//注意这里判断字符串是否相等要用equals()方法
System.out.println("登录成功");
break;
}
else{
if(i==2){
System.out.println("您的账户已被锁定,请联系管理员");
}
else{
System.out.println("密码用户名错误请重新输入,您还剩下"+(2-i)+"次机会");}
}
}
}
}
练习:统计字符串次数
package day8;
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
int bigcount=0;
int smallcount=0;
int numcount=0;
for (int i = 0; i < str.length(); i++) {
char c=str.charAt(i);
if(c>'a'&&c<'z'){
smallcount++;
}
else if(c>'A'&&c<'Z'){
bigcount++;
}
else if(c>'0'&&c<'9'){
numcount++;
}
}
System.out.println("大写字母有"+bigcount+"个");
System.out.println("小写字母有"+smallcount+"个");
System.out.println("数字字符有"+numcount+"个");
}
}
练习:拼接字符串
package day8;
public class test1 {
public static void main(String[] args) {
int arr1[]={1,2,3};
System.out.println(arrtoString(arr1));
}
public static String arrtoString(int[] arr){
String result="[";
if(arr==null){
return "";
}
if(arr.length==0){
return "[]";
}
for (int i = 0; i < arr.length; i++) {
if(i==arr.length-1){
result=result+arr[i];
}
else{
result=result+arr[i]+",";
}
}
result=result+"]";
return result;
}
}
练习:字符串反转
快捷键:shift+F6批量修改
package day8;
public class test1 {
public static void main(String[] args) {
System.out.println(reverse("abc"));
}
public static String reverse(String str){
String result="";
for (int i = str.length() - 1; i >= 0; i--) {
char c=str.charAt(i);
result=result+c;
}
return result;
}
}
练习:金额转换
package day8;
public class test1 {
public static void main(String[] args) {
String money=capitalNum(2135);
String[] danwei={"佰","拾","万","仟","佰","拾","元"};
for(int i=0;i<7;i++){
char c=money.charAt(i);
System.out.print(c);
System.out.print(danwei[i]);
}
}
//定义方法将数字变为中文字
public static String capitalNum(int num){
//int[] numArr=new int[];
String[] Capital={"零","壹","贰","叁","肆","伍","六","七","八","九"};
String str="";
//获取输入数字中的每一位数字
while(true){
int ge=num%10;
str=Capital[ge]+str;
num=num/10;//去掉个位
if(num==0){
break;
}
}
int count=7-str.length();
for (int i = 0; i < count; i++) {
str="零"+str;
}
return str;
}
}
练习:身份证信息查看
package day8;
public class test1 {
public static void main(String[] args) {
String str="120109200112095013";
//获取年份
String str1=str.substring(6,10);
//月份
String str2=str.substring(10,12);
//日
String str3=str.substring(12,14);
System.out.println("个人信息为:");
System.out.println("出生日期为:"+str1+"年"+str2+"月"+str3+"日");
//获取性别
char gender=str.charAt(16);//'3'
//利用ascall码将字符gender转为数字,'0'--48
int gendernum=gender-48;
if(gendernum%2==0){
System.out.println("性别为:女");
}
else{
System.out.println("性别为:男");
}
}
}
练习:敏感词替换
package day8;
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
String str=sc.next();
String[] sql={"cnm","sb","nt",",mlgb"};
for (int i = 0; i < sql.length; i++) {
str=str.replace(sql[i],"****");
}
System.out.println(str);
}
}
练习:对称字符串
package day8;
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
//使用StringBuilder的场景
//字符串的拼接
//字符串的反转
//将字符串进行反转
String str2=new StringBuilder().append(str).reverse().toString();//链式编程
if(str.equals(str2)){
System.out.println("该字符串是对称字符串");
}else{
System.out.println("不是对称字符串");
}
}
}
字符串相关操作总结
练习:调整字符串
套路:如果我们看到要修改字符串的内容,可以有两个办法:
方法一:用subString进行截取,把左边的字符截取出来拼接到右侧去
package day8;
public class test1 {
public static void main(String[] args) {
String strA="abcde";
String strB="abcde";
boolean flag=check(strA,strB);
if(flag){
System.out.println("True");
}
else{
System.out.println("flase");
}
}
//定义一个方法检查字符串是否相等
public static boolean check(String strA,String strB){
for (int i = 0; i < strB.length(); i++) {
state(strA);
if(strA.equals(strB)){
return true;
}
}
return false;
}
//定义一个方法,旋转字符串
public static String state(String str){
char c=str.charAt(0);
String str1=str.substring(1);//Substring截取字符串
str=str1+c;
return str;
}
}
方法二:可以把字符串先变成一个字符数组,然后调整字符数组里面的数据,最后再把字符数组变成字符串。
package day8;
public class test1 {
public static void main(String[] args) {
String strA="abcde";
String strB="ABC";
boolean flag=check(strA,strB);
if(flag){
System.out.println("True");
}
else{
System.out.println("flase");
}
}
//定义一个方法检查字符串是否相等
public static boolean check(String strA,String strB){
for (int i = 0; i < strB.length(); i++) {
state(strA);
if(strA.equals(strB)){
return true;
}
}
return false;
}
//定义一个方法,旋转字符串
public static String state(String str){
char[] arr=str.toCharArray();//将字符串转化为字符数组
char c=arr[0];
for(int i=1;i<arr.length;i++){
arr[i-1]=arr[i];
}
arr[arr.length-1]=c;
String str1=arr.toString();
return str1;
}
}