声明两个变量: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. IE中的fireEvent和webkit中的dispatchEvent

    拿浏览器的click事件来说: 在IE浏览器中如果一个element没有注册click事件,那么直接调用的话会出现异常!当然如果你注册了没有什么可说的. 那么如果使用fireEvent来处理,clic ...

  2. Linux Socket过程详细解释(包括三次握手建立连接,四次握手断开连接)

    我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览器浏览网页时,浏览器的进程怎么与web 服务器通信的?当你用QQ聊天时,QQ进程怎么与服务器或你好友所在的QQ进程通信?这些都得靠s ...

  3. 插入图片<img>

    格式: <img src="图片地址" alt="下载失败时的替换文本" title="提示文本"> src:图像的位置 alt ...

  4. js实现表格信息的删除和添加

    制作一个表格,显示班级的学生信息. 要求: 1. 鼠标移到不同行上时背景色改为色值为 #f2f2f2,移开鼠标时则恢复为原背景色 #fff 2. 点击添加按钮,能动态在最后添加一行 3. 点击删除按钮 ...

  5. C# 常用日期函数

    我想知道取的时期是几月.几日,然后做一些统计,上网找了一些方法. --DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1 ...

  6. https://my.oschina.net/huangyong/blog/161419

    https://my.oschina.net/huangyong/blog/161419

  7. ORACLE十进制与十六进制的转换

    十进制与十六进制的转换 十进制-->十六进制 select to_char(100,'XX') from dual; 十六进制-->十进制select to_number('7D','XX ...

  8. JavaScript:JavaScript中常见获取对象元素的方法

    介绍: javascript中常见的3种获取元素的方法,分别是通过元素ID.通过标签名字和通过类名字来获取 操作如下: 1.getElementById DOM提供了一个名为getElementByI ...

  9. MVC项目实践,在三层架构下实现SportsStore-05,实现导航

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  10. int[] List<int> 排序

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