/*
* 模拟登陆:给三次机会,并提示还有几次
* 用户名和密码都是admin
* 分析:
* 1、模拟登陆需要键盘录入,scanner
* 2、给3次机会,需要循环 for
* 3、提示有几次,需要判断 if
*
*
*/
public class Demo {
//定义用户和密码
static String s = "admin";
static String p = "admin";
public static void main(String[] args) {
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
//3次机会
for(int x=0;x<3;x++){
System.out.println("请输入用户名:");
String user = sc.nextLine();
System.out.println("请输入密码");
String password = sc.nextLine();
//判断用户输入是后正确
if(user.equals(s)&&password.equals(p)){
System.out.println("登陆成功");
break;//跳出循环
}else{
if(x==2){
System.out.println("你输入错误次数过多,账号已锁定");
}else{
System.out.println("你还有"+(2-x)+"次机会");
}
}
}
}
}
/*
* 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数。
* ABCDEabcd123456!@#$%^
* 分析:字符串是有字符组成的,而字符的值都是有范围的,通过范围来判断是否包含该字符
* 如果包含就让计数器变量自增
*
*/
public class Demo {
public static void main(String[] args) {
//遍历字符串
String s = "ABCDEabcd123456!@#$%^";
int big = 0;
int small = 0;
int num = 0;
int other = 0;
//遍历字符串并取值
for(int x=0;x<s.length();x++){
//取出每个字符
char c = s.charAt(x);
//通过比较取出各种符合条件的字符
if(c>='A' && c<='Z'){
big++;
}else if(c>='a' && c<='z'){
small++;//
}else if(c>='0' && c<='9'){
num++;
}else{
other++;
}
}
//打印每个种类的个数
System.out.println("大写字母有:"+big+"个\t"+"小写字母有:"+small+"个\t"+"数字有:"
+num+"个\t"+"其他有:"+other+"个\t");
}
}
/*
** A:案例演示
* 需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
* 链式编程:只要保证每次调用完方法返回的是对象,就可以继续调用
*/
public class Demo {
public static void main(String[] args) {
String s = "aBCDEabcd";
//截取首字母并转换
String s1 = s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase());
//打印
System.out.println(s);
System.out.println(s1);
}
}
/*
* * * A:案例演示
* 需求:把数组中的数据按照指定个格式拼接成一个字符串
* 举例:
* int[] arr = {1,2,3};
* 输出结果:
* "[1, 2, 3]"
*
分析:
1,需要定义一个字符串"["
2,遍历数组获取每一个元素
3,用字符串与数组中的元素进行拼接
*用完方法返回的是对象,就可以继续调用
*/
public class Demo {
public static void main(String[] args) {
int[] a ={1,2,3};
String s = "[";
//遍历数组
for(int x=0;x<a.length;x++){
//拼接“[”
if(x==a.length-1){
s = s+a[x]+"]";
}else{
s=s+a[x]+",";
}
}
System.out.println(s);
}
}