用java记事本编写以下要求,大于6位小于20位的汉字组成,编写程序实现地址合法性检测,输入正确则提示“OK”, 否则根据实际情况提示“地址格式不符合要求”
时间: 2023-02-20 16:46:30 浏览: 47
我可以为您提供一段示例代码:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入地址:");
String address = in.nextLine();
int len = address.length();
if (len >= 6 && len <= 20) {
System.out.println("OK");
} else {
System.out.println("地址格式不符合要求");
}
}
相关问题
用java记事本编写以下要求,用户在新增地址信息的时候,需要输入自己所在的地址,要求是大于6位小于20位的汉字组成,编写程序实现地址合法性检测,输入正确则提示“OK”, 否则根据实际情况提示“地址格式不符合要求”
public class AddressCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的地址:");
String address = sc.nextLine();
int len = address.length();
if (len > 6 && len < 20) {
for (int i = 0; i < len; i++) {
char c = address.charAt(i);
if (c < 0x4E00 || c > 0x9FA5) {
System.out.println("地址格式不符合要求");
break;
}
}
System.out.println("OK");
} else {
System.out.println("地址格式不符合要求");
}
}
}
用java记事本编写以下代码,用户在注册id的时候,需要输入自己的注册id,要求是验证由两位数字、一个连字符再加 5 位数字组成的 ID 号。编写程序实现id合法性检测,输入正确则提示“OK”, 否则根据实际情况提示“id格式不符合要求”
import java.util.Scanner;public class IDVerification {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入注册ID:");
String id = scanner.nextLine();
// 校验ID格式
if(id.matches("\\d{2}-\\d{5}")) {
System.out.println("OK");
} else {
System.out.println("ID格式不符合要求");
}
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)