写一方法计算实现任意个整数之和.在主调函数中调用该函数,实现任意个数之和。(使用params参数)
2024-10-19 03:27:45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
int sum = Test(, , , , );
Console.WriteLine("多个整数之和为:{0}", sum);
Console.ReadKey();
} static int Test(params int[] arr)
{
int sum = ;
for (int i = ; i < arr.Length; i++)
{
sum += arr[i];
}
return sum;
}
}
}
改进后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{ Console.WriteLine("请输入需要相加的整数个数");
int i = Convert.ToInt32(Console.ReadLine());
int[] readint = new int[i];
for (int k = ; k < i; k++)
{
Console.WriteLine("请输入需要相加的整数");
readint[k] = Convert.ToInt32(Console.ReadLine());
}
int sum = Test(readint);
Console.WriteLine("多个整数之和为:{0}", sum);
Console.ReadKey();
} static int Test(params int[] arr)
{
int sum = ;
for (int i = ; i < arr.Length; i++)
{
sum += arr[i];
}
return sum;
}
}
}
最新文章
- 手把手教从零开始在GitHub上使用Hexo搭建博客教程(三)-使用Travis自动部署Hexo(1)
- AAS代码第2章
- javascript 红宝书笔记之函数、变量、参数、作用域
- 让那些为Webkit优化的网站也能适配IE10(转载)
- 学的一点点ps
- javap(反汇编命令)详解【转】
- xshell无法连接centos 6.6的问题
- jQuery easyui 提示框
- Swift学习笔记三
- 《how to design programs》第11章自然数
- android笔试题
- 【转】Android 网络通信框架Volley简介(Google IO 2013)
- 处理win7任务栏通知区域图标异常问题
- Nuget发布教程
- [Swift]LeetCode895. 最大频率栈 | Maximum Frequency Stack
- mysql创建用户与pymsql模块
- halcon 特征测量
- 从session中获取当前用户的工具类
- android 数据库更新
- Vue + Spring Boot从零开始搭建个人网站(一) 之 项目前端Vue.js环境搭建