声明两个变量: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. 关于使用UDP(TCP)跨局域网,NAT穿透的心得

    前言:        最近我用java做了一个C/S的类似QQ之类的IM系统(即时通讯系统),遇到了不能跨局域网通讯的问题,经过在网上,和书上查阅了一些资料,了解了一些情况,现在就总结一下我的解决方案 ...

  2. Sql server中内连接语句

    数据库中学生表和课程表如下: 内连接sql语句: select a.studentName,a.studentAge,b.courseName from student a inner join co ...

  3. LeetCode Binary Search Tree Iterator

    原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...

  4. 测试环境下将centos6.8升级到centos7的操作记录(转)

    在测试环境下安装openstack,由于在centos6下安装openstack,针对源的问题有很多,安装起来很不顺利! 但是在centos7下安装却很顺利,所以考虑将服务器由centos6升级到ce ...

  5. python中新式类和经典类的区别

    1).python在类中的定义在py2-3版本上是使用的有新式类和经典类两种情况,在新式类和经典类的定义中最主要的区别是在定义类的时候是否出现引用object;如:经典类:Class 类名::而新式类 ...

  6. Python模块(pickle)

    pickle 序列化和反序列化 序列化作用 序列化使用 cPickle使用例 Python提供一个标准的模块,称为pickle.使用它你可以在一个文件中储存任何Python对象,之后你又可以把它完整无 ...

  7. 堡垒机 paramiko 自动登陆代码

    #!/usr/bin/env python # Copyright (C) - Robey Pointer <robeypointer@gmail.com> # # This file i ...

  8. Perl Print Win32 Console Windows 控制台 print Unicode 问题

    参考资料: https://stackoverflow.com/questions/15224400/perl-on-windows-problems-with-encoding https://te ...

  9. RMAN-06059: expected archived log not found, loss of archived log compromises recoverability

    归档日志被物理删除后执行rman操作报错: RMAN> backup database plus archivelog; Starting backup at -JUL- :: current ...

  10. fifo manage

    typedef struct { TEntry *header; TEntry *tailer; uint16 alloc; uint16 release;}TPoolCtrl;