標籤:必須 使用者 輸入密碼 一個 電話 practice public AC 常用
定一個字串可以使用string類和stringbuffer類。
string類提供了大量的操作字串的方法,常用的如下:
獲得字串的長度: length()。
比較字串:equals()。
連結字串:concat()。
提取字串:substring()。
搜尋字串:indexOf()。
拆分字串:spilt()。
常用的stringbuffer類提供的方法如下:
轉換成string類型:tostring()。
連結字串:append()。
插入字串:intsert()。
一些案例:
public class Demo_1 { public static void main(String[] args) { // TODO Auto-generated method stub String pwd; @SuppressWarnings("resource") Scanner input =new Scanner(System.in); do{ System.out.println("請輸入使用者名稱:"); String name = input.next(); System.out.println("請輸入密碼:"); pwd= input.next(); if(pwd.length()<6){ System.out.println("密碼長度不夠"); }else{ System.out.println("輸入正確"); System.out.println("歡迎你!"+name); break; } }while(pwd.length()!=6); }
boolean flag ; public void panduan(String sfz , String sjh,String zj) { flag=false; int index=sjh.lastIndexOf("-"); if(!(sfz.length()==16||sfz.length()==18)){ System.out.println("社會安全號碼必須是16位或18位!"); } else if(sjh.length()<11){ System.out.println("手機號必須是11位!"); }else if (index!=-1&&index!=0&&index!=4&&zj.length()!=13) { System.out.println("有線電話號碼區必須為4位,電話號碼必須是7位!"); }else{ System.out.println("註冊成功"); flag=true; } }調用:public static void main(String[] args) { @SuppressWarnings("resource") Scanner input=new Scanner(System.in); Practice2 pd =new Practice2(); do { System.out.println("請輸入身份證:"); String sfz = input.next(); System.out.println("請輸入手機號碼:"); String sjh = input.next(); System.out.println("請輸入有線電話號:"); String zj = input.next(); pd.panduan(sfz, sjh, zj); } while (pd.flag==false); }
小白的java學習之路 “ 字串”