packageHeima.ZiFuChuan;importJava_Learning.util.Scanner;publicclassLoadtest{publicstaticvoidmain(String[] args){for(int i =0;i<3;i++){//定义用户名和密码String username ="110";String password ="119";//键盘录入Scanner s =newScanner(System.in);System.out.println("请输入用户名:");String name = s.nextLine();Scanner p =newScanner(System.in);System.out.println("请输入密码:");String pass = p.nextLine();//比较内容是否相等if(name.equals(username)&& pass.equals(password)){System.out.println("输入正确!");break;}else{if(2-i==0){System.out.println("你的账户已锁定!");}else{System.out.println("您的输入有误,"+"还有"+(2-i)+"次机会");}}}}}
案例2:遍历字符串
s.length() //字符串长度
s.charAt(i) //字符串第i-1个字符
importJava_Learning.util.Scanner;publicclassBianLiZiFuChuan{publicstaticvoidmain(String[] args){//键盘录入Scanner sc =newScanner(System.in);System.out.println("请输入一串字符:");String s = sc.nextLine();//遍历字符串for(int i =0;i<s.length();i++){System.out.print(s.charAt(i));}}}