用户登录
这里的用户名和密码是已知的,主要考察equals的用法
import java.util.Scanner;
/*
- 用户登录
- 已知用户名和密码共有三次登陆机会,登陆后给出相应提示
- */
public class test5 {
public static void main(String[] args) {
int i = 0;
while (i < 3){
Scanner sc = new Scanner(System.in);
System.out.println(“请输入用户名:”);
String username = sc.nextLine();
System.out.println(“请输入密码:”);
int password = sc.nextInt();
if(username.equals(“admin”) && password ==123){
System.out.println(“已成功登陆!”);
break;
} else {
if (i == 2){
System.out.println(“你的账户已锁定!”);
break;
}
System.out.println(“你还有” + (2-i) + “次机会!”);
}
i++;
}
}
}
遍历字符串统计字符个数
考察charAt()的用法
import java.util.Scanner;
/*
- 遍历字符串并且统计大写字符小写字符和数字的数量
*字符串是从0开始的 - */
public class test6 {
public static void main(String[] args) {
int daxie = 0;
int xiaoxie = 0;
int shuzi = 0;
Scanner sc = new Scanner(System.in);
System.out.println(“please input a string:”);
String a = sc.nextLine();
for (int i = 0; i < a.length(); i++){
char b = a.charAt(i);
System.out.print(b + " ");
if (i == a.length()-1){
System.out.println();
}
if(b > ‘A’ && b < ‘Z’){
daxie++;
}
else if(b > ‘a’ && b < ‘z’){
xiaoxie++;
}
else if(b > ‘0’ && b < ‘9’){
shuzi++;
}
}
System.out.println(“大写字母:” +daxie +"个 " +“小写字母:”+xiaoxie + "个 " + “数字:” +shuzi +“个”);
}
}