第七章 常用类
1.Scanner
import java.util.Scanner;
public class Cest {
public static void main(String[] args){
String sc= new Scanner(System.in).nextLine();
System.out.println("Line:"+sc);
}
}
2.Object类
1.toString()方法
2.equals()方法
public class Test {
public static void main(String[] args){
Human h1=new Human("Lily",30);
Human h2=new Human("Lily",30);
System.out.println(h1==h2);//false,不是一个地址
System.out.println(h1.equals(h2));//false,Object类默认比较地址,想比较内容,要重写equals方法
/*
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Human human = (Human) o;
return age == human.age && Objects.equals(name, human.name);
}
*/
System.out.println(h1.equals(h2));//true,重写后
}
}
3.String类
public class Test {
public static void main(String[] args){
String s1=new String("hello");
System.out.println(s1);//hello
char[] value={'h','e','l','l','o'};
String s2=new String(value);
System.out.println(s2);//hello
String s3=new String(value,0,3);
System.out.println(s3);//hel
String s4="hello";//最常用的
System.out.println(s4);
}
}
public class Test {
public static void main(String[] args){
String s1=new String("hello");
String s2=new String("hello");
String s3="hello";
String s4="hello";
System.out.println(s1==s2);//false
System.out.println(s3==s4);//true
System.out.println(s1==s4);//false
}
}
案例1:模拟登录,给三次机会,并且提示还有几次
import java.util.Scanner;
public class Test {
public static void main(String[] args){
String name="admin";
String password="admin";
for(int x=0;x<3;x++){
System.out.println("请输入用户名:");
String nm=new Scanner(System.in).nextLine();
System.out.println("请输入用户密码:");
String pwd=new Scanner(System.in).nextLine();
if(nm.equals(name) && pwd.equals(password)){
System.out.println("登录成功");
break;
}else{
if(x-2==0){
System.out.println("账号被锁定,请与管理员联系");
}else{
System.out.println("登录失败,你还有"+(2-x)+"次机会");
}
}
}
}
}
案例2:遍历字符串
String s="abcde";
for (int i=0;i<s.length();i++){
System.out.println(s.charAt(i));
}
案例3:把数组里的数据拼成字符串
int[] arr={1,2,3};
String s="";
s+="[";
for (int i=0;i<arr.length;i++){
if(i==arr.length-1){
s+=arr[i];
break;
}else
s+=arr[i];
s+=", ";
}
s+="]";
System.out.println(s);
案例4:字符串反转
public static void main(String[] args){
System.out.println("请输入一个字符串:");
String m=new Scanner(System.in).nextLine();
for (int i=m.length()-1;i>=0;i--){
System.out.print(m.charAt(i));
}
4.StringBuilder类
public static void main(String[] args){
StringBuilder s1=new StringBuilder();
System.out.println("s1:"+s1);
System.out.println(s1.length());
StringBuilder s2=new StringBuilder("helloworld");
System.out.println("s2:"+s2);
System.out.println(s2.length());
}
public class Test {
public static void main(String[] args){
StringBuilder s1=new StringBuilder();
s1.append("hello");
s1.append(100);
//链式编程
s1.append(200).append(300).append(400);
System.out.println("s1:"+s1);
s1.reverse();
System.out.println("s1:"+s1);
}
}
public static void main(String[] args){
StringBuilder s1=new StringBuilder();
s1.append("hello");
System.out.println("s1:"+s1);
String s2= s1.toString();//StringBuilder转String
System.out.println("s2:"+s2);
String s3="hello";
StringBuilder s4= new StringBuilder(s3);//String转StringBuilder
System.out.println("s4:"+s4);
}
案例:数组里的数据转字符串拼接,字符串反转:
public class Test {
public static void main(String[] args){
int[] arr={1,2,3};
StringBuilder s1=new StringBuilder();
s1.append("[");
for(int i=0;i<arr.length;i++){
if (i==arr.length-1){
s1.append(arr[i]);
break;
}else{
s1.append(arr[i]).append(", ");
}
}
s1.append("]");
System.out.println("s1:"+s1.toString());
}
}
public class Test {
public static void main(String[] args){
System.out.println("请输入一个字符串:");
String sc=new Scanner(System.in).nextLine();
StringBuilder s =new StringBuilder(sc);
s.reverse();
System.out.println(s.toString());
}
}