声明两个变量: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. drop、 truncate 、 delete

    相同点: truncate和不带where子句的delete, 以及drop都会删除表内的数据     不同点: 1. truncate和 delete只删除数据不删除表的结构 drop语句将删除表的 ...

  2. Housse Robber II | leetcode

    可以复用house robber的代码,两趟dp作为两种情况考虑,选最大值 #include <stdio.h> #define MAX 1000 #define max(a,b) ( ( ...

  3. 20145211 《Java程序设计》实验报告三:敏捷开发与XP实践

    实验内容 使用 git上传代码 使用 git相互更改代码 实现代码的重载 XP基础 XP核心实践 相关工具 一.git上传代码 这一部分是与我的partner合作的,详见他的博客- 20145326蔡 ...

  4. Magento修改邮件模板内容

    Magento 默认邮件模板 都是带着官方的标志和一些官方的基本信息.为了建立品牌形象我们需要把邮件模板中的所有官方信息换成自己的信息.修改步骤如下: 1.找到Magento的邮件模板文件(这里以 e ...

  5. Android笔记-获取图片

     1. 图片放在sdcard中,根据路径获得: Bitmap imageBitmap = BitmapFactory.decodeFile(path) (path 是图片的路径,跟目录是/sdcard ...

  6. css3背景色渐变

    <style> .test { width: 200px; height: 200px; background: -moz-linear-gradient(top, #8fa1ff, #3 ...

  7. JavaScript基础总结三部曲之一

      JavaScript是一种基于对象的编程语言,基于对象指的是所有的对象已经由浏览器提供好了,用户直接使用就可以了. 另外,JS是嵌入在HTML语法之中的编程语言. PS:虽然是基于对象的语言,但是 ...

  8. Bugfree实用心得_转

    转自:http://blog.csdn.net/benkaoya/article/details/8719257 本博下有许多实用技巧 1. 什么是问题跟踪系统 问题跟踪系统(Issue Tracki ...

  9. 防止IE不支持console.log报错

    function log(msg){ if (window["console"]){//判断是否是IE console.log(msg); } }

  10. REST Security with JWT using Java and Spring Security

    Security Security is the enemy of convenience, and vice versa. This statement is true for any system ...