package DEMO2.DAY10_demo06_练习_模拟登陆; import java.util.Scanner; public class Demo { public static void main(String[] args) { String username = "admin"; String password = "admin"; Scanner sc = new Scanner(System.in); for (int i = 1; i <= 3; i++) { System.out.println("请输入用户名:"); String inputusername = sc.next(); System.out.println("请输入密码:"); String inputpassword = sc.next(); if (username.equals(inputusername) && password.equals(inputpassword)) { System.out.println("登陆成功"); break; } else { if (i == 3) { System.out.println("三次机会已用完,账号锁定"); } else { System.out.println("用户名或密码错误,你还有" + (3 - i) + "次机会"); } } } } }首字母转大写 需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
package DEMO2.DAY10_demo07_练习_首字母转大写; public class Demo { public static void main(String[] args) { String s1 = "abcd"; //获取第一个字母 String s2 = s1.substring(0,1); System.out.println(s2); //获取第二个字符串之后的值 String s3 = s1.substring(1); System.out.println(s3); //拼接结果 String result = s2.toUpperCase()+s3.toLowerCase(); System.out.println("拼接之后的结果:"+result); } }数组转字符串 需求:把数组中的数据按照指定个格式拼接成一个字符串
package DEMO2.DAY10_demo08_练习_数组转字符串; public class Demo { public static void main(String[] args) { int[] arr = {1,2,3}; //"[1, 2, 3]" String result = "["; for (int i = 0; i < arr.length; i++) { if(i == arr.length-1) { result += arr[i]+" "; }else{ result += arr[i]+", "; } } result += "]"; System.out.println(result); } }字符串反转 需求:把字符串反转 举例:键盘录入"abc" 输出结果:"cba"
package DEMO2.DAY10_demo09_练习_字符串反转; public class Demo { public static void main(String[] args) { String s1 = "abc"; for (int i = s1.length()-1; i >=0 ; i--) { System.out.print(s1.charAt(i)); } } }统计字符的个数 题目:获取指定字符串中,大写字母、小写字母、数字的个数。
package DEMO2.DAY10_demo10_练习_统计字符的个数; public class Demo { public static void main(String[] args) { String s1 = "EDGabcD999"; int bigCount = 0;//大写 int smallCount = 0;//小写 int numberCount = 0;//数字 for (int i = 0; i < s1.length(); i++) { char charI = s1.charAt(i); //ascii //charI >= 65 && charI <= 90 if(charI >= 'A' && charI <='Z'){ bigCount ++; }else if(charI >= 'a' && charI <= 'z'){ smallCount ++; }else{ numberCount ++; } } System.out.println("大写: "+bigCount); System.out.println("小写: "+smallCount); System.out.println("数字: "+numberCount); } }大串中查找小串出现的次数 需求:查询大字符串中,出现指定小字符串的次数。 代码1
package DEMO2.DAY10_demo11_练习_大串中查找小串出现的次数; public class Demo { public static void main(String[] args) { String c = "woaijavawoaijavawoaijavawoaijavahhhjava"; //"java" //找到第一次出现的下标 4 //int index =s.indexOf("java"); //System.out.println(index); int count = getCount("java", c); System.out.println(count); } public static int getCount(String target,String s){ //记录出现的次数 int count = 0; //查找第一次出现的位置 int index = s.indexOf(target); //如果还存在字符串 while(index !=-1){ count++;//次数+1 //第一次循环:woaijavawoaijavawoaijavawoaijavahhhjava //第二次循环:woaijavawoaijavawoaijavahhhjava //第三次循环:woaijavawoaijavahhhjava s= s.substring(index+target.length()); index =s.indexOf(target); } return count; } }
最终的代码
package DEMO2.DAY10_demo11_练习_大串中查找小串出现的次数; public class Demo { public static void main(String[] args) { String c = "woaijavawoaijavawoaijavawoaijavahhhjava"; //"java" //找到第一次出现的下标 4 //int index =s.indexOf("java"); //System.out.println(index); int count = getCount("java", c); System.out.println(count); } public static int getCount(String target,String s){ //记录出现的次数 int count = 0; //查找第一次出现的位置 int index = 0;//s.indexOf(target); //如果还存在字符串 //将index的赋值放到循环的判断条件里面 while((index =s.indexOf(target)) !=-1){ count++;//次数+1 //第一次循环:woaijavawoaijavawoaijavawoaijavahhhjava //第二次循环:woaijavawoaijavawoaijavahhhjava //第三次循环:woaijavawoaijavahhhjava s= s.substring(index+target.length()); // index =s.indexOf(target); } return count; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)