声明两个变量:int n1 = 10, n2 = 20;要求将两个变量交换,最后输出n1为20,n2为10。交换两个变量,使用第三个变量!

  class Program
{
static void Main(string[] args)
{
int n1 = ;
int n2 = ;
int temp = n1;
n1 = n2;
n2 = temp;
Console.WriteLine("n1:{0},n2:{1}", n1, n2);
Console.ReadKey();
}
}

声明两个变量:int n1 = 10, n2 = 20;要求将两个变量交换,最后输出n1为20,n2为10。交换两个变量,不使用第三个变量!

 class Program
{
static void Main(string[] args)
{
int n1 = ;
int n2 = ;
n1 = n1 + n2;
n2 = n1 - n2;
n1 = n1 - n2;
Console.WriteLine("n1:{0},n2:{1}", n1, n2);
Console.ReadKey();
}
}

利用执行过程--压栈

    class Program
{
static void Main(string[] args)
{
int n1 = ;
int n2 = ;
n2 = n1 + (n1 = n2) * ;
Console.WriteLine("n1:{0},n2:{1}", n1, n2);
Console.ReadKey();
}
}

声明两个变量,交换两个变量,使用方法! 

  class Program
{
static void Main(string[] args)
{
int n1 = ;
int n2 = ;
Swap(ref n1, ref n2);//方法存根快捷键Ctrl+K+M建立方法 ref表示按引用传递
Console.WriteLine("n1:{0},n2:{1}", n1, n2);
Console.ReadKey();
} private static void Swap(ref int num1, ref int num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
}

计算1+2-3+4-5+6-7+.........100值。

 class Program
{
static void Main(string[] args)
{
int sum = ;
for (int i = ; i <=; i++)
{
if (i == || i % == )
{
sum += i;
}
else
{
sum -= i;
}
}
Console.WriteLine(sum);
Console.ReadKey();
}
}

请用户输入一个字符串,计算字符串中的字符个数,并输出。(“你好ycz” 的字符个数为5)字符串的Length属性表示字符串中字符的个数,无论中文字符还是英文字符,一个字符就是一个字符,不是字节数。

  class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个字符串");
string msg = Console.ReadLine();
Console.WriteLine("字符个数为:{0}", msg.Length);
Console.ReadKey();
}
}

定义方法来实现:计算两个数的最大值。提示:方法有几个参数?返回值是什么?      ···········定义方法时一定要有参数和返回值!

  class Program
{
static void Main(string[] args)
{
int max = GetMax(, );
Console.WriteLine("最大值为:{0}", max);
Console.ReadKey();
} static int GetMax(int n1, int n2)
{
return n1 > n2 ? n1 : n2;
}
}

计算任意多个数间的最大值(提示:params)。

 class Program
{
static void Main(string[] args)
{
int max = GetMax(, , , , , );
Console.WriteLine("最大值为:{0}", max);
Console.ReadKey();
} static int GetMax(params int[] nums)//params可变参数
{
int max = nums[];
for (int i = ; i < nums.Length; i++)
{
if (nums[i] > max)
max = nums[i];
}
return max;
}
}

用方法来实现:计算1-100之间的所有整数的和。

  class Program
{
static void Main(string[] args)
{
int sum = GetSum(, );
Console.WriteLine(sum);
Console.ReadKey();
} static int GetSum(int StartNumber, int EndNumber)
{
int sum = ;
for (int i = StartNumber; i <= EndNumber; i++)
{
sum += i;
}
return sum;
}
}

用方法来实现:计算1-100之间的所有奇数的和。

 class Program
{
static void Main(string[] args)
{
int sum = GetOddSum(, );
Console.WriteLine(sum);
Console.ReadKey();
} static int GetOddSum(int StartNumber, int EndNumber)
{
int sum = ;
for (int i = StartNumber; i <= EndNumber; i++)
{
if (i % != )
{
sum += i;
}
}
return sum;
}
}

用方法来实现:判断一个给定的整数是否为“质数”;

    class Program
{
static void Main(string[] args)
{
string s;
Console.WriteLine("请输入一个正整数:");
int n = Convert.ToInt32(Console.ReadLine());
if (IsZhiShu(n))
{
s = "这个数是质数";
Console.WriteLine(s);
Console.ReadKey();
}
else
{
s = "这个数不是质数";
Console.WriteLine(s);
Console.ReadKey();
}
} static bool IsZhiShu(int num)
{
bool b = true;
for (int i = ; i <= Math.Sqrt(num); i++) //对num开根号
{
if (num % i == )
{
b = false;
}
}
return b;
}
}

用方法实现:计算1-100之间所有质数的和。

 class Program
{
static void Main(string[] args)
{
int sum = ;
for (int i = ; i <= ; i++)
{
// 对于每个数字判断是否是一个质数
if (IsZhiShu(i))
{
sum += i;
}
}
Console.WriteLine(sum);
Console.ReadLine();
} static bool IsZhiShu(int num)
{
bool b = true;
for (int i = ; i <= Math.Sqrt(num); i++) //对num开根号
{
if (num % i == )
{
b = false;
}
}
return b;
}
}

用方法来实现:有一个数组,找出其中最大值并输出。不能调用数组的Max()方法。

  class Program
{
static void Main(string[] args)
{
int[] arrInt = { , , , , , , , };
int max = GetMax(arrInt);
Console.WriteLine(max);
Console.ReadLine();
} static int GetMax(int[] arr)
{
int max = arr[];
for (int i = ; i < arr.Length; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
return max;
}
}

用方法来实现:有一个数组,找出其中最大值并输出。调用数组的Manx()方法

  class Program
{
static void Main(string[] args)
{
int[] arrInt = { , , , , , , , };
int max = arrInt.Max();
Console.WriteLine(max);
Console.ReadLine();
}
}

用方法来实现:有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出字符数最多的字符串。

    class Program
{
static void Main(string[] args)
{
string[] arrName = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
string maxName = GetMaxString(arrName);
Console.WriteLine(maxName);
Console.ReadLine();
} static string GetMaxString(string[] arrName)
{
string maxName = arrName[];
for (int i = ; i < arrName.Length; i++)
{
if (arrName[i].Length > maxName.Length)
{
maxName = arrName[i];
}
}
return maxName;
}
}

用方法实现:请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }。要求:计算结果如果有小数,则显示小数点后两位(四舍五入)。Math.Round()

    class Program
{
static void Main(string[] args)
{
int[] arrInt = { , , , , , , , , , };
Console.WriteLine(GetAvg(arrInt));
Console.ReadLine();
} static double GetAvg(int[] arr)
{
int sum = ;
for (int i = ; i < arr.Length; i++)
{
sum += arr[i];
}
return Math.Round((double)sum / arr.Length, ); //Math.Round()对数字执行四舍五入,保存两位小数。注意*两个整数相除结果还是整数,可以将一个强制转换成double类型
}
}

通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。

 class Program
{
static void Main(string[] args)
{
int[] arrInt = { , , , , , , , , , };
for (int i = ; i < arrInt.Length - ; i++)
{
for (int j = arrInt.Length - ; j > i; j--)
{
if (arrInt[j] < arrInt[j - ])
{
int temp = arrInt[j];
arrInt[j] = arrInt[j - ];
arrInt[j - ] = temp;
}
}
} for (int i = ; i < arrInt.Length; i++)
{
Console.WriteLine(arrInt[i]);
}
Console.ReadLine();
}
}

自己编写函数实现十进制转成二进制

 class Program
{
static void Main(string[] args)
{ while (true)
{
Console.WriteLine("请输入一个十进制数字:");
int num = Convert.ToInt32(Console.ReadLine());
string result = GetBinaryNumber(num);
Console.WriteLine(result);
}
} private static string GetBinaryNumber(int num)
{
////拼接字符串
//StringBuilder sb = new StringBuilder();
//while (num >= 2)
//{
// //商
// int shang = num / 2;
// //余数
// int yushu = num % 2;
// sb.Append(yushu);
// //把本次计算的商赋值给num变量
// num = shang;
//}
//sb.Append(num);
//return sb.ToString(); List<string> list = new List<string>();
while (num >= )
{
//商
int shang = num / ;
//余数
int yushu = num % ;
list.Add(yushu.ToString());
//把本次计算的商赋值给num变量
num = shang;
}
list.Add(num.ToString());
list.Reverse();
return string.Join("", list.ToArray());
}
}

十进制转成二进制(系统方法)

    class Program
{
static void Main(string[] args)
{
DateTime begin = DateTime.Now;
int i = ;
string j = Convert.ToString(i, );//j就是转换后的二进制了!!
Console.WriteLine(j);
DateTime end = DateTime.Now;
Console.WriteLine("计算耗时:" + (end - begin) + "秒");
Console.ReadLine();
}
}

判断瑞年

 class Program
{
static void Main(string[] args)
{ while (true)
{
Console.WriteLine("请输入一个年份:");
int year = Convert.ToInt32(Console.ReadLine());
bool b = IsLeapYear(year);
Console.WriteLine(b);
}
} private static bool IsLeapYear(int year)
{
if (year%== && year%!= || year%==)
{
return true;
}
else
{
return false;
}
}
}

乘法口诀表

    class Program
{
static void Main(string[] args)
{
for (int i = ; i <= ; i++)
{
for (int j = ; j <= i; j++)
{
Console.Write("{0}*{1}={2} \t", j, i, j * i); // \t 水平制表位(horizeontal tab),将光标移到最接近8的倍数的位置,使得后面的输入从此开始。换句话说,让所有的数据都紧跟在制表符后面输出。
}
Console.WriteLine();
}
Console.ReadKey();
}
}

自己编写一个函数,实现类似.net中Trim()函数的功能;去掉字符串两端的空格

    class Program
{
static void Main(string[] args)
{
//string msg = " 你 好 吗? ";
//Console.WriteLine("========" + msg.Trim() + "===========");
//Console.ReadKey(); string msg = " 你 好 吗? ";
Console.WriteLine("========" + MyTrim(msg) + "===========");
Console.ReadKey();
} private static string MyTrim(string msg)
{
int start = ;
int end = msg.Length - ;
//寻找第一个不是空白符的字符的索引,并保存到start变量中
while (start < msg.Length)
{
if (!char.IsWhiteSpace(msg[start]))
{
break;
}
start++;
} //寻找最后一个不是空白符的字符的索引,并保存到end变量中 while (end >= start)
{
if (!char.IsWhiteSpace(msg[end]))
{
break;
}
end--;
}
return msg.Substring(start, end - start + );
}
}

随机生成10个1-100内的非重复的偶数放入ArrayList集合中。ArrayList集合:using System.Collections;

 class Program
{
static void Main(string[] args)
{
int count = ;
ArrayList arrList = new ArrayList();
Random rdm = new Random();//()内不能放参数,不然会产生一样的,里面是种子,种子确定,产生的数是一样的!
while (arrList.Count < )
{
//Random rdm = new Random();//这句话不能放到这里,系统用当前时间做种子
count++;
int n = rdm.Next(, );
if (n % == && !arrList.Contains(n))
{
arrList.Add(n);
}
}
for (int i = ; i < arrList.Count; i++)
{
Console.WriteLine(arrList[i]);//快捷键 cw+Table
}
Console.WriteLine("循环了{0}次", count);
Console.ReadKey();
}
}

有如下字符串:【"患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"】。需求:①请统计出该字符中“咳嗽”一词的出现次数,以及每次“咳嗽”出现的索引位置。②扩展(*):统计出每个字符的出现次数。    JavaScript indexOf() 方法

  class Program
{
static void Main(string[] args)
{
string msg = "患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?";
string keywords = "咳嗽";
int count = ;
int index = ;//记录索引
while ((index = msg.IndexOf(keywords, index)) != -)//使用IndexOf(),该方法返回在整个字符串中,指定的字符或字符串第一次出现的索引位置,如果没有找到指定的字符或者字符串则返回-1,如果找到了返回索引位置
{
count++;
Console.WriteLine("第{0}次,出现【咳嗽】,索引时{1}", count, index);
index = index + keywords.Length;
}
Console.WriteLine("【咳嗽】一词,共出现了{0}次,", count);
Console.ReadKey();
}
}

将字符串"      hello world,你 好 世界 !      "两端空格去掉,并且将其中的所有其他空格都替换成一个空格,输出结果为:"hello world,你 好 世界 !"。 Trim用法  Split用法Join用法

 class Program
{
static void Main(string[] args)
{
string msg = " Hello World 你 好 世界 ";
msg = msg.Trim();
string[] words = msg.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string full = string.Join(" ", words);
Console.WriteLine(full);
Console.ReadKey();
}
}

统计数组中不重复元素的个数

 class Program
{
static void Main(string[] args)
{
int[] arrInt = {,,,,,,,,,,,,,,,,,,}; //统计不重复个数的元素
int count = ;
for (int i = ; i < arrInt.Length; i++)
{
bool b = true;
for (int j = ; j <=i; j++)
{
if(arrInt[i]==arrInt[j] && i!=j)
{
b = false;
break;
}
}
if (b)
{
count++;
}
}
Console.WriteLine("不重复的元素的个数:{0}",count);
Console.ReadKey();
}
}

去掉重复元素方法一

    class Program
{
static void Main(string[] args)
{
int[] arrInt = { , , , , , , , , , , , , , , , , , , , , , , , , ,,,};
//对数组进行排序
Array.Sort(arrInt); List<int> list = new List<int>();
for (int i = ; i < arrInt.Length - ; i++)
{
if (arrInt[i] != arrInt[i + ])
{
list.Add(arrInt[i]);
}
}
list.Add(arrInt[arrInt.Length - ]);
for (int i = ; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
Console.ReadKey();
}
}

去掉重复元素方法二

    class Program
{
static void Main(string[] args)
{
int[] arrInt = { , , , , , , , , , , , , , , , , , , , , , , , , , , , };
//对数组进行排序
Array.Sort(arrInt);
arrInt=arrInt.GroupBy(p => p).Select(p => p.Key).ToArray();
foreach (var item in arrInt)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}

制作一个控制台小程序。要求:用户可以在控制台录入每个学生的姓名,当用户输入quit(不区分大小写)时,程序停止接受用户的输入,并且显示出用户输入的学生的个数,以及每个学生的姓名。并统计姓王的个数!

  class Program
{
static void Main(string[] args)
{
List<string> ListNames = new List<string>();//初始化一个集合
int count = ;
while (true)
{
Console.WriteLine("请输入姓名:");
string name = Console.ReadLine();
if (name[] == '王')
{
count++;
}
if (name.ToLower() == "quit")
{
break;
}
ListNames.Add(name);
}
Console.WriteLine("你共输入了:{0}个名字", ListNames.Count);
foreach (string name in ListNames)
{
Console.WriteLine(name);
}
Console.WriteLine("姓王的同学的个数:{0}", count);
Console.ReadKey();
}
}

01---Net基础加强的更多相关文章

  1. 01: tornado基础篇

    目录:Tornado其他篇 01: tornado基础篇 02: tornado进阶篇 03: 自定义异步非阻塞tornado框架 04: 打开tornado源码剖析处理过程 目录: 1.1 Torn ...

  2. 后端 - Lession 01 PHP 基础

    目录 Lession 01 php 基础 1. php 基础 2. php 变量 3. php 单引号 和 双引号区别 4. 数据类型 5. 数据类型转换 6. 常量 7. 运算符 8. 为 fals ...

  3. Jam's balance HDU - 5616 (01背包基础题)

    Jim has a balance and N weights. (1≤N≤20) The balance can only tell whether things on different side ...

  4. 086 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 03 面向对象基础总结 01 面向对象基础(类和对象)总结

    086 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 03 面向对象基础总结 01 面向对象基础(类和对象)总结 本文知识点:面向对象基础(类和对象)总结 说明 ...

  5. 075 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 07 综合案例-数组移位-主方法功能4的实现

    075 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 07 综合案例-数组移位-主方法功能4的实现 本文知识点:综合案例-数组移位-主方法功能4的实现 说明:因为 ...

  6. 074 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 06 综合案例-数组移位-主方法功能3的实现

    074 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 06 综合案例-数组移位-主方法功能3的实现 本文知识点:综合案例-数组移位-主方法功能3的实现 说明:因为 ...

  7. 073 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 05 综合案例-数组移位-主方法功能1和2的实现

    073 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 05 综合案例-数组移位-主方法功能1和2的实现 本文知识点:综合案例-数组移位-主方法功能1和2的实现 说 ...

  8. 072 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 04 综合案例-数组移位-在指定位置处插入数据方法

    072 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 04 综合案例-数组移位-在指定位置处插入数据方法 本文知识点:综合案例-数组移位-在指定位置处插入数据方法 ...

  9. 071 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 03 综合案例-数组移位-显示数组当中所有元素的的方法

    071 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 03 综合案例-数组移位-显示数组当中所有元素的的方法 本文知识点:综合案例-数组移位-显示数组当中所有元素 ...

  10. 070 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 02 综合案例-数组移位-从键盘接收数据

    070 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 02 综合案例-数组移位-从键盘接收数据 本文知识点:综合案例-数组移位-从键盘接收数据 说明:因为时间紧张 ...

随机推荐

  1. Selenium2学习-018-WebUI自动化实战实例-016-自动化脚本编写过程中的登录验证码问题

    日常的 Web 网站开发的过程中,为提升登录安全或防止用户通过脚本进行黄牛操作(宇宙最贵铁皮天朝魔都的机动车牌照竞拍中),很多网站在登录的时候,添加了验证码验证,而且验证码的实现越来越复杂,对其进行脚 ...

  2. windows server 2008服务器IIS绑定阿里云域名

    一.打开Internet 信息服务(IIS)管理器   二.将你的网站放到服务器目录下,比如D盘下的WWW文件夹.   三.在IIS中,添加网站,网站的物理路径指向第二部中创建的网站.   五.在绑定 ...

  3. LeetCode Integer to English Words

    原题链接在这里:https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to it ...

  4. Medical image computing

    Processing and analysis of medical images using computer comprises the following: image formation an ...

  5. linux 安装apahce的configure: error: APR not found. Please read the documentation解决办法

    1.下载所需软件包: 下载apr并配置 wget http://apache.freelamp.com/apr/apr-1.4.2.tar.gz 下载apr ./configure –prefix=/ ...

  6. ucenter 通信成功后 apps.php无误后 该做的事

    比如你的系统有个登陆功能,在本系统登陆之前要先执行如下代码,先把apps.php里的所有站点先登陆一遍 <meta charset="utf8"> <?php i ...

  7. BI如何让企业管理从信息化迈向智能化 ——暨珠海CIO协会成立大会圆满召开

    2016年8月27日,珠海CIO协会成立大会在珠海度假村酒店成功举办.此次会议由奥威软件等数家公司共同协办.珠海市信息协会秘书长周德元先生.广东省首席信息官协会秘书长周庆林先生.珠海市首席信息官协会会 ...

  8. int[] List<int> 排序

    ; List<,,,,,,}; ,,,,}; List<int> result = allingInts.ToList(); result.Sort(); allingInts = ...

  9. iOS UITableView的分割线短15像素,移动到最左边的方法(iOS8)

    有好几个朋友问我ios 分割线端了一些 如何解决,于是我就写一篇博客吧.为什么我说是少了15像素呢?首先我们拖拽一个默认的tableview 控件! 看下xcode5 面板的inspector(检查器 ...

  10. CPU虚拟化技术(留坑)

    留坑~~~ 不知道这个是这么实现的 CPU虚拟化技术就是单CPU模拟多CPU并行,允许一个平台同时运行多个操作系统,并且应用程序都可以在相互独立的空间内运行而互不影响,从而显著提高计算机的工作效率.虚 ...