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
/
sumNumberInStrings.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
25 lines (22 loc) · 1.26 KB
main
Breadcrumbs
JAVApRracticeBasics
/
sumNumberInStrings.java
Copy path
Top
File metadata and controls
Code
Blame
25 lines (22 loc) · 1.26 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
import java.util.*; // 导入java.util包,包含Scanner等工具类
public class sumNumberInStrings { // 定义主类
public static void main(String[] args) { // 主方法,程序入口
Scanner input = new Scanner(System.in); // 创建Scanner对象,用于读取用户输入
int sum = 0; // 初始化求和变量sum,用于累加数字字符的值
String str = input.nextLine(); // 读取用户输入的第一行字符串
// 使用while循环,当输入的不是"end"时继续执行
while (!str.equals("end")) {
// 遍历当前字符串的每一个字符
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i); // 获取字符串中第i个字符
if (Character.isDigit(c)) { // 判断该字符是否是数字
sum += Character.getNumericValue(c); // 如果是数字,将其数值加到sum中
}
}
System.out.println(sum); // 输出当前字符串中所有数字字符的累加和
sum = 0; // 重置sum为0,准备处理下一行输入
str = input.nextLine(); // 读取下一行输入
}
input.close();
}
}
You can’t perform that action at this time.