Java自學-String(1)
阿新 • • 發佈:2021-08-12
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)); } } }