登录验证demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static void main(String[] args) {
String rightname = "zhangsan";
String rightpassword = "123";
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
//共有三次机会
System.out.println("请输入用户名:");
String username = sc.next();
System.out.println("请输入密码:");
String password = sc.next();
if (username.equals(rightname) && password.equals(rightpassword)) {
System.out.println("登录成功!");
break;
} else {
if (i == 2) {
System.out.println(username + "已被锁定请联系管理员进行操作!");
} else {
System.out.println("登录失败" + "你还剩" + (2 - i) + "次机会!");
}
}
}
}

数字转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public static void main(String[] args) {
//键盘先录入 判断是否合法
Scanner sc = new Scanner(System.in);
int money;
while (true) {
System.out.println("请输入金额:");
money = sc.nextInt();
if (money >= 0 && money <= 9999999) {
break;
} else {
System.out.println("输入不合法,请重新输入!");
}
}
//定义一个变量记录金额
String moneyStr = "";
while (true) {
int ge = money % 10;
String number = getCapitalNumber(ge);
moneyStr = number + moneyStr;
money = money / 10;
if (money == 0) {
break;
}
}
System.out.println(moneyStr);
//补零操作
int count = 7 - moneyStr.length();
for (int i = 0; i < count; i++) {
moneyStr = "零" + moneyStr;
}
System.out.println(moneyStr);
//插入单位
String result = "";
String[] arr = {"佰", "拾", "万", "千", "佰", "拾", "元"};
for (int i = 0; i < moneyStr.length(); i++) {
char ch = moneyStr.charAt(i);
result = result + ch + arr[i];
}
System.out.println(result);
}

//数字与大写字母相对应
public static String getCapitalNumber(int number) {
String[] arr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
return arr[number];
}

数字拼接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 43, 21};
System.out.println(ArrToString(arr));
}
public static String ArrToString(int[] arr) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
sb.append(arr[i]);
} else {
sb.append(arr[i] + ",");
}
}
sb.append("]");
return sb.toString();
}