C# 7 函数 青歌赛打分 天气预报
函数:
数据类型--变量常量--运算符表达式--语句(顺序,分支,循环)--数组--函数
程序里的函数:能完成一个相对独立功的代码块。
数学里的函数:高度抽象。
函数四要素:函数名,输入,输出,加工
函数定义:
[static] 返回类型 函数名(输入参数列表)
{
//函数体——加工
}
函数调用:
函数名(参数); ——适合于调用无返回值的函数
数据类型 变量 = 函数名(参数); ——适合于调用有返回值的函数。
今天要掌握的东西:
1.函数是个什么东西?
2.能体会到函数的好处:可重用,可分工,清晰易读
3.记住函数的四要素。
4.会定义简单的函数,会调用这个函数。
例:函数
class Program //函数多于是加上了class ,找分析每块函数别找错地方
{
static void M1ain(string[] args)
{
ShowHeader();
string city = ShuRu();
string jieguo = YunSuan(city);
Console.WriteLine("天气预报仅做参考:" + jieguo);
}
private static string YunSuan(string city)
{
//天气
string[] s = new string[] { "晴", "晴", "晴", "晴", "晴", "晴", "晴", "晴", "晴", "晴", "晴", "晴", "晴", "晴", "晴", "阴", "多云", "小雨", "中雨", "大雨", "暴雨", "雷阵雨", "小雪", "中雪", "大雪", "霾", "雾" };
Random rand = new Random();
int n = rand.Next(s.Length); //天气的下标.
string tq = s[n];
//气温
int c = rand.Next(70);
c -= 30;
string jieguo = "明天的天气情况:" + city + ",天气:" + tq + ",气温:" + c;
return jieguo;
}
private static string ShuRu()
{
Console.Write("城市:");
string city = Console.ReadLine();
return city;
}
private static void ShowHeader()
{
Console.WriteLine("*************天气预报****************");
Console.WriteLine("********淄博气象台权威发布****************");
Console.WriteLine("********如果出现异常 ,纯属偶然****************");
Console.WriteLine("*****仅做个人测试与爱好使用,不要用作商业运营**********");
}
}
例:青歌赛打分
class Class1
{
static void Main(string[] args)
{
int[] a = new int[10];
//亮分
ShuRu(a);
//排序
PaiXu(a);
//运算求平均
double avg = YunSuan(a);
//输出显示
ShuChu(a, avg);
}
private static void ShuChu(int[] a, double avg)
{
Console.WriteLine("去掉两个最高分:" + a[0] + "和" + a[1]);
Console.WriteLine("去掉两个最低分:" + a[a.Length - 1] + "和" + a[a.Length - 2]);
Console.WriteLine("该选手最终得分为:" + avg);
}
private static double YunSuan(int[] a)
{
//求总分
int sum = 0;
for (int i = 2; i <= a.Length - 3; i++)
{
sum += a[i];
}
//求平均
double avg = (1.0 * sum) / (a.Length - 4);
return avg;
}
private static void PaiXu(int[] a)
{
for (int i = 1; i <= a.Length - 1; i++)
{
for (int j = 1; j <= a.Length - i; j++)
{
if (a[j] > a[j - 1])
{
int temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
}
}
}
}
private static void ShuRu(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
Console.Write("请第" + (i + 1) + "号评委亮分:");
a[i] = Convert.ToInt32(Console.ReadLine());
}
}
}
C# 7 函数 青歌赛打分 天气预报的更多相关文章
- foreach 循环遍历 以及函数的应用
foreach( 对集合每个元素的引用 in 集合 ){ } 举例: int[] a = new int[5]{1,2,3,4,5};foreach( int b in a ){ //b就是a中的每个 ...
- foreach使用和函数
2016-04-25 一.foreach( 对集合每个元素的引用 in 集合 ) { } int[] a = new int[5]{1,2,3,4,5}; foreach( int b in a ) ...
- 20160418 while,switch,do..while的使用
9 一.While循环 示例:求100以内所有数的和 Int i=1;//初始条件 Int sum=0; While(i<=100)//循环条件 { Sum+=i;//循环体 i++;//状态改 ...
- C#。总结
数据类型--变量与常量--运算符与表达式--语句(if,for)--数组--函数--结构体一.数据类型: (一)内建类型 整型(int short long byte uint ushort ulon ...
- C# 10 总复习
数据类型--变量与常量--运算符与表达式--语句(if,for)--数组--函数--结构体 一.数据类型: (一)内建类型 整型(int short long byte uint ushort ulo ...
- C#语言小结
数据类型--变量与常量--运算符与表达式--语句(if,for)--数组--函数--结构体 一.数据类型:(一)内建类型整型(int short long byte uint ushort ulong ...
- C#阶段小结
一.数据类型: (一)内建类型: 整型(int ,short, long ,byte ,uint ,ushort, ulong ,sbyte): 浮点型(double float decimal): ...
- C#整理5——break与continue.及数组
一.break与continue.这两个关键字一般放在循环的花括号里面使用.break——结束整个循环.continue——结束本次循环,进入下次循环. break的案例: using System; ...
- C# 5 break continue 球员成绩 彩票 选班长
二.新课: 1.break与continue. 这两个关键字一般放在循环的花括号里面使用. break--结束整个循环. continue--结束本次循环,进入下次循环. break的案例: ...
随机推荐
- 在Linux系统上限制远程登录的IP
在Linux系统上限制远程登录的IP,使用系统自带的配置文件. /etc/hosts.allow /etc/hosts.deny 匹配原则 先allow 后deny.
- C# 调用存储过程传入表变量作为参数
首先在SQLServer定义一个自定义表类型: USE [ABC] GO CREATE TYPE [ABC].[MyCustomType] AS TABLE( ) NOT NULL, ) NULL, ...
- dl dt dd标签
<dl>标记定义了一个定义列表,定义列表中的条目是通过使用<dt>标记(“definition title”,定义标题)和<dd>标记(“definition de ...
- C++中如何split字符串(转)
#include <iostream> #include <sstream> #include <string> using namespace std; int ...
- C#创建Windows服务与安装-图解
1.创建windows服务项目
- What is SignalR and Why Should I Use It?
原文地址: What is SignalR and why should I use it? When I first heard about SignalR, I was not sure what ...
- Win7 64位系统上配置使用32位的Eclipse(转)
Win7 64位系统上配置使用32位的Eclipse 博客分类: Eclipse eclipse 最近工作电脑换成了64位的win7系统,之前个人电脑上安装的jdk和Eclipse都是32位的.而新 ...
- Android系统五大布局详解Layout
我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前, 视图都是由一个一个的组件构成的.组件就是我们常见的Button.TextEdit ...
- D - Specialized Four-Digit Numbers
Description Find and list all four-digit numbers in decimal notation that have the property that the ...
- $ cd `dirname $0` 和PWD%/* shell变量的一些特殊用法
在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的.因为他返回当前路径的".". $0:当前Shell程序的文件名dirname $0,获取当前Shell程序 ...