package ZZQ;
/*
* ATM机取款案例,银行输入密码不能
超过5次,超过5次账号锁定,密码匹
配成功显示登录成功!
*/
import java.util.Scanner;
public class lxt1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String password = "12345";
System.out.println("********欢迎来到中国银行********");
/*
* i=0 剩余4次
* i=1 剩余3次
* i=2 剩余2次
* i=3 剩余1次
* i=4 剩余0次
*/
for (int i = 0; i < 5; i++) {
System.out.println("请输入密码:");
String pwd = input.next();
if (pwd.equals(password)) {
System.out.println("登录成功!");
break;
} else {
if (i==4) {
System.out.println("输入密码错误超过5次账户锁定");
} else {
System.out.println("您还有" + (4-i) + "次机会");
}
}
}
}
}
package ZZQ;
/*
* 把字符串的首字母转大写,其他字母转小写,同时将每个字符使用_下划线隔开
* 原字符串:youAreWelcome
转换后字符串:Y_o_u_a_r_e_w_e_l_c_o_m_e
*/
public class lxt2 {
public static void main(String[] args) {
//1.获取到首字母,将首字母转换大写
//2.获取到其他字母,其他字母转小写
//3.拼接两个字符串
System.out.println(String.join("_",String.valueOf("youAreWelcome".charAt(0)).toUpperCase().concat("youAreWelcome".substring(1).toLowerCase()).split("")));
}
}
package ZZQ;
/*
* 字符串倒置
* 如键盘录入 “我爱你”
* 输出结果:"你爱我”
*
* 0 1 2 2 1 0
* 我 爱 你 你 爱 我
*/
public class lxt3 {
public static void main(String[] args) {
String z = "我爱你";
String result = "";
for (int i = z.length()-1; i >= 0; i--) {
result += z.charAt(i);
}
System.out.println(result);
}
}
package ZZQ;
/*
* 我爱中国,你是我的中国,我的中国有你
*/
import java.util.Scanner;
public class lxt4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入字符串:");
String line1 = input.next();
System.out.println("请输入要找的字符串:");
String line2 = input.next();
int count = getLine2InLine1Count(line2,line1);
System.out.println("中国个数为: " + count);
}
public static int getLine2InLine1Count(String line2,String line1) {
int count = 0;
int index = 0;
while ((index = line1.indexOf(line2)) != -1) {
count++;
line1 = line1.substring(index + line2.length());
}
return count;
}
}
package ZZQ;
import java.util.Scanner;
/*
* 使用String和StringBuffer实现字符串倒置
*/
public class lxt5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入字符串: ");
String str = input.next();
String[] strArr = str.split(" ");
for (int i = 0; i < strArr.length; i++) {
StringBuffer mo = new StringBuffer(strArr[i]);
mo = mo.reverse();
System.out.println(mo);
}
}
}
package ZZQ;
/*
* int[] arr = {33,22,55,11,66}
分别使用String和StringBuffer按照如下格式输出
arr[33, 22, 55, 11, 66]
*/
public class lxt6 {
public static void main(String[] args) {
//StringBuffer方法
StringBuffer str = new StringBuffer();
str.append("int[] arr = {33,22,55,11,66}");
str.delete(str.indexOf("i"), str.indexOf("a"));
str.replace(str.indexOf(" "), str.indexOf("}"), "[33, 22, 55, 11, 66]");
str.deleteCharAt(str.indexOf("}"));
System.out.println(str);
System.out.println(arrayToString(new int[] {33,22,55,11,66}));
}
//String 方法
public static String arrayToString(int[] arr) {
String result = "";
result += "arr[";
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
result += arr[i];
} else {
result += arr[i] + ", ";
}
}
result += "]";
return result;
}
}
package ZZQ;
import java.util.Arrays;
import java.util.Scanner;
/*
* 把字符串中的字符进行排序 dfebcga
*/
public class lxt7 {
@SuppressWarnings("resource")
public static void main(String[] args) {
String line = new Scanner(System.in).next();
System.out.println(sortString(line));
}
public static String sortString(String str) {
byte[] bys = str.getBytes();
Arrays.sort(bys);
return new String(bys);
}
}