
I have added the numbers using an array −
int[] num = new int[] {1, 25, 1, 55, 1};Now loop through and find for 1, if 1 is there, 6 then increment the variable that counts the occurrence −
采用JSP开发的办公自动化产品、基于B/S结构,运行环境:JDK v1.5、Tomcat v5.5、MySQL v4.1,三者均为以上版本其他相关内容:可视化流程设计: 流程支持串签、会签和分支流程,可以设置流程节点的修改、删除权限,并可指定流程中各个用户在表单中可以填写的域。智能表单所见即所得设计: 智能设计,自动在数据库中生成表格,方便优化程序 公共交流: 集论坛、博客、聊天室于一体文件柜:C
0
foreach(int j in num) {
if (j == 1) {
cal++;
}
}The following is the code to count the number of 1’s in the entered number.
Live Demo
using System;
public class Demo {
public static void Main() {
int cal = 0;
int[] num = new int[] {1, 25, 1, 55, 1};
Console.WriteLine("Numbers:");
for (int i = 0; i < 5; i++) {
Console.WriteLine(num[i]);
}
foreach (int j in num) {
if (j == 1) {
cal++;
}
}
Console.WriteLine("Number of 1's: ");
Console.WriteLine(cal);
Console.ReadLine();
}
}Numbers: 1 25 1 55 1 Number of 1's: 3
以上就是C#程序:计算输入数字中1的个数的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号