foreach使用和函数
2016-04-25
一.foreach( 对集合每个元素的引用 in 集合 )
{
}
int[] a = new int[5]{1,2,3,4,5};
foreach( int b in a )
{
//b就是a中的每个元素
}
★ 注意:
1.foreach只能对集合进行遍历。不能用下标;冒泡排序不能用。
2.foreach在操作集合的时候,只能读不能改。保证集合稳定,
3.foreach操作Dictionary<T,T》 //哈希表只能用foreach去遍历。
Dictionary<string,string> dic = new Dictionary<string,string>();
//...添加值
foreach( KeyValuePare<string,string> b in dic)
{
}
4.foreach操作List<T>
List<int> list = new List<int>();
//添加值
foreach( int b in list )
{
}
5.foreach能够操作IEnumarable(IEnumarator)下的子级集合。
6.foreach使用范围较小,不能取代for
2.函数:
主要的功能就是为了让代码结构更加良好。
函数是——实现相对独立功能的程序代码模块(程序段).
函数的四要素:函数名,输入,输出,运算
有的函数没有输入,函数名后的小括号中可以不写东西,但必须要有小括号。
有的函数没有返回,在函数名的左侧不要写数据类型了,写void即可.
定义语法:
static 返回类型 函数名(形参列表(输入类型))
{
函数体,运算
}
static int Max(int a,int b)
{
if(a>b)
{
return a;
}
else
{
return b;
}
}
调用语法:
数据类型 变量名 = 函数名(实参列表(传去的值));
int n = Max(7,8);
形参与实参一定要一一对应(个数,类型)
传值
把实参的数据做个复本,送到函数的形参中去。
一般基本类型(int,double,char,bool,DateTime)都是值类型,他们默认情况下会传值。
传址
把实参的地址传组函数的形参中去。形参与实参共用同一个数据空间。
一般引用类型都是传地址的。
1.青歌赛打分:
namespace ConsoleApplication1
{
class Class2
{
static void Main(string[] arge)
{
//青歌赛打分,10个评委,最大值,最小值,去掉最高分和最低分的平均分
int []a=new int [];
int max = , min = , sum = ;
//输入
//运算
a = shuru(a);
max =Max(a );
min=Min (a);
sum = Sum(a); //输出
shuchu(a, max, min, sum);
}
static void shuchu(int[] a,int max,int min,int sum)
{
double avg = 1.0*(sum - max - min) / (a.Length - );
Console.WriteLine("最高分是{0}\t 最低分是{1}\t平均分{2}",max,min,avg);
}
static int[] shuru(int[] a)
{
for (int i = ; i < a.Length;i++ )
{
Console.WriteLine("第{0}位评委评分是:",i+);
a[i] = Convert.ToInt32(Console .ReadLine ());
}
return a;
}
static int Max(int[] a)
{
int zd = ;
foreach (int b in a )
{
if (zd <b)
{
zd = b;
}
}
return zd ;
}
static int Min(int[] a)
{
int zx = ;
foreach (int b in a)
{
if (zx>b)
{
zx=b;
}
}
return zx;
}
static int Sum(int[] a)
{
int he = ;
foreach (int b in a)
{
he = he + b;
}
return he;
} }
}

2. 题目:给定一个数组,给出一个数,查找一下是否存在,用二分法
class Class3
{
static void Main(string[] ssdgrd)
{
//给定一个数组,给出一个数,查找一下是否存在
int[] a = new int[] {,,,,,,,,, };
//输入
Console.WriteLine("输入要查找的数:");
int n = Convert.ToInt32(Console .ReadLine ());
//排序
a=paixu(a); //查找
bool zhaodaole = chazhao(a, n);
//输出
if (zhaodaole ==true )
{
Console.WriteLine("找到啦");
}
if (zhaodaole ==false )
{
Console.WriteLine("木找到");
}
}
static int[] paixu(int[] a)
{
for (int i = ; i < a.Length;i++ )
{
for (int j = i; j < a.Length - ;j++ )
{
if (a[i]>a[j+])
{
int t = a[i];
a[i]=a[j+];
a[j + ] = t;
}
}
}
return a;
}
static bool chazhao(int [] a,int n)
{
bool find = false;
int start=,end=a.Length -,mid;
for (; ; )
{
mid =(start+end )/;
if (n == a[mid])
{
find = true;
break;
}
else
{
if (n>a[mid])
{
start = mid + ;
}
if (n<a[mid])
{
end = mid - ;
}
else if (end <start )
{
find = false;
break;
}
}
}
return find;
}
foreach使用和函数的更多相关文章
- js foreach、map函数
语法:forEach和map都支持2个参数:一个是回调函数(item,index,input)和上下文: •forEach:用来遍历数组中的每一项:这个方法执行是没有返回值的,对原来数组也没有影响: ...
- VUE错误记录 - 品牌后台练习 search(); 数组 splice forEach push 箭头函数
methods:{ add(){ var car = { id: this.id, name: this.name, ctime: new Date()}; this.list.push(car); ...
- JavaScript中foreach、map函数
语法:forEach和map都支持2个参数:一个是回调函数(item,index,input)和上下文: •forEach:用来遍历数组中的每一项:这个方法执行是没有返回值的,对原来数组也没有影响: ...
- swift 的高阶函数的使用代码
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- AngularJs angular.forEach、angular.extend
angular.forEach 调用迭代器函数取每一项目标的集合,它可以是一个对象或数组.迭代器函数与迭代器(value.key)一起调用,其中值是一个对象属性或数组元素的值,而数组元素是对象属性的关 ...
- [译]ES6箭头函数和它的作用域
原文来自我的前端博客: http://www.hacke2.cn/arrow-functions-and-their-scope/ 在ES6很多很棒的新特性中, 箭头函数 (或者大箭头函数)就是其中值 ...
- forEach 方法 (Array) (JavaScript)
为数组中的每个元素执行指定操作. 语法 array1.forEach(callbackfn[, thisArg]) 参数 参数 定义 array1 必选.一个数组对象. callbackfn 必选.最 ...
- smarty中增加类似foreach的功能自动加载数据方法
第一步:在Smarty_Compiler.class.php的_compile_tag函数中增加: 复制代码 代码如下: //加载数据的开始标签case 'load': $this->_push ...
- php : 匿名函数(闭包) [二]
摘自: http://www.cnblogs.com/yjf512/archive/2012/10/29/2744702.html php的闭包(Closure)也就是匿名函数.是PHP5.3引入的. ...
随机推荐
- Tomcat的类加载器
看完了Java类装载器,我们再来看看应用服务器(Tomcat)对类加载器的使用,每个应用服务器都有一套自己的类加载器体系,从而与Java的类加载器区别开以达到自己与应用程序隔离的目的.Tomcat的类 ...
- redhat samba安装配置
突然想要用,好久没配过这玩意了,再次配置一次,记录一下过程. 1.挂载镜像 mount -o loop ~/Desktop/RHEL_5.5\ i386\ DV.iso /mnt ...
- DataUml Design 教程7 - 数据库生成模型
DataUml Design支持数据库生成模型,并支持外键关系,能够根据外键自动生成类与类之间的关系. 目前DataUML Design支持MS Server.MY SQL.Oracle和Access ...
- Kafka具体解释五、Kafka Consumer的底层API- SimpleConsumer
1.Kafka提供了两套API给Consumer The high-level Consumer API The SimpleConsumer API 第一种高度抽象的Consumer API,它使用 ...
- 【实用】如何在windows下快速截图?
如何在windows下快速截图? 快速截图是很多人的需求.截图的工具和方案也很多,但是,这里给出一个通用的,被大众认为最高效的一个解决方案. 我们都知道键盘上有一个"prt sc" ...
- github桌面工具commit不了解决
这样发现github上还是空文件, 因为要重新添加过 1. git add -A 添加所有文件 2. git commit -m "xxx" 3. git push -u ...
- 【Mac + Appium + Python3.6学习(三)】之IOS自动化测试环境配置
在做这一节之前先配置我的另一篇文章所需要安装的前提准备条件:<[Mac + Appium学习(一)]之安装Appium环境前提准备> 一.安装IOS自动化测试环境 配置环境: Appium ...
- Java基础12 类型转换与多态(转载)
类型检查 Java的任意变量和引用经过类型声明(type declaration),才能使用.我们之前见过对象数据.类数据.方法参数.方法返回值以及方法内部的自动变量,它们都需要声明其类型.Java是 ...
- js保留几位小数
function reservedDecimal(val, digit) { return Number(val).toFixed(digit);} 调用 reservedDecimal(10,2); ...
- Linux下汇编语言------计算n的阶乘
用高级语言比方C语言写个阶乘非常easy,那如今就来熟悉下Linux使用AT&T汇编格式写一个程序来计算阶乘吧 写法一是 使用跳转指令实现.写法二是使用函数来实现 约定:本程序没有将结果打印在 ...