本文主要是介绍Java自学-String(1),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Java自学-String(1)
1、String概述
2、String构造方法
package lesson01;
/**
* Author: Gu Jiakai
* Date: 2021/8/12 17:36
* FileName: StringDemo01
* Description:
*/
public class StringDemo01 {
public static void main(String[] args) {
String s1 = new String();
System.out.println("s1:"+s1);
char[] chs={'a','b','c'};
String s2 = new String(chs);
System.out.println("s2:"+s2);
byte[] bys={97,98,99};
String s3 = new String(bys);
System.out.println("s3:"+s3);
String s4="abcd";
System.out.println("s4:"+s4);
}
}
3、String对象的特点
4、字符串的比较
5、用户登录
package lesson01;
import java.util.Scanner;
/**
* Author: Gu Jiakai
* Date: 2021/8/12 17:54
* FileName: StringDemo02
* Description:
*/
public class StringDemo02 {
public static void main(String[] args) {
String username="19130104";
String password="123456";
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名:");
String name=sc.nextLine();
System.out.println("请输入密码:");
String pwd=sc.nextLine();
if(name.equals(username)&&pwd.equals(password)){
System.out.println("登录成功!");
break;
}else{
if(2-i==0){
System.out.println("你的账户已被锁定,请联系管理员!");
}else {
System.out.println("登录失败,你还有" + (2 - i) + "次机会!");
}
}
}
}
}
6、遍历字符串
package lesson01;
import java.util.Scanner;
/**
* Author: Gu Jiakai
* Date: 2021/8/12 18:08
* FileName: StringDemo03
* Description:
*/
public class StringDemo03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s = sc.nextLine();
for (int i = 0; i < s.length(); i++) {
System.out.println(s.charAt(i));
}
}
}
这篇关于Java自学-String(1)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!