Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
KeyandBoy
JAVApRracticeBasics
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
JAVApRracticeBasics
/
checkSSN.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
32 lines (28 loc) · 1.59 KB
main
Breadcrumbs
JAVApRracticeBasics
/
checkSSN.java
Copy path
Top
File metadata and controls
Code
Blame
32 lines (28 loc) · 1.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import java.util.*; // 导入java.util包,包含Scanner等工具类
public class checkSSN { // 定义主类,用于验证SSN(社会保险号)格式
public static void main(String[] args) { // 主方法,程序入口
Scanner scanner = new Scanner(System.in); // 创建Scanner对象,用于读取用户输入
// StringBuilder sb = new StringBuilder(); // (注释掉的代码)可用于动态构建字符串
System.out.println("please input the SSN"); // 提示用户输入SSN
// sb.append(scanner.nextLine()); // (注释掉的代码)将用户输入添加到StringBuilder
String ssn = scanner.nextLine(); // 读取用户输入的SSN字符串
// 定义SSN的正则表达式格式:
// ^\d{3}[-/ ]\d{2}[-/ ]\d{4}$
// 解释:
// ^\d{3} - 开头是3位数字
// [-/ ] - 分隔符(-、/或空格)
// \d{2} - 接着是2位数字
// [-/ ] - 同上分隔符
// \d{4}$ - 结尾是4位数字
String SSN = "^\\d{3}[-/ ]\\d{2}[-/ ]\\d{4}$";
// 检查输入的SSN是否符合格式要求:
// 1. 长度是否为11字符(如123-45-6789)
// 2. 是否匹配正则表达式(允许用-/或空格分隔)
if (ssn.length() == 11 || ssn.matches(SSN)) {
System.out.print("yes"); // 格式正确,输出"yes"
} else {
System.out.print("no"); // 格式错误,输出"no"
}
scanner.close(); // 关闭Scanner,释放资源
}
}
You can’t perform that action at this time.