15个小练习
1.声明两个变量:int n1=10,n2=20;要求将两个变量的值交换:n1=20,n2=10.不使用第三方变量,完整代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _01练习
{
class Program
{
static void Main(string[] args)
{
#region 声明两个变量:int n1=10,n2=20;要求将两个变量的值交换:n1=20,n2=10.不使用第三方变量
int n1 = 10, n2 = 20;
n1 = n1 + n2;
n2 = n1 - n2;
n1 = n1 - n2;
Console.WriteLine("交换后的n1= "+n1);
Console.WriteLine("交换后的n2= "+n2);
#endregion
}
}
}
2.声明两个变量:int n1=10,n2=20;要求封装一个方法将两个变量的值交换,注意要使用ref,完整代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _02练习
{
class Program
{
static void Main(string[] args)
{
#region 声明两个变量:int n1=10,n2=20;要求将两个变量的值交换:n1=20,n2=10.不使用第三方变量
int n1 = 10, n2 = 20;
Swap(ref n1,ref n2);
Console.WriteLine(n1);
Console.WriteLine(n2);
Console.ReadLine();
#endregion
}
private static void Swap(ref int n1,ref int n2)
{
int temp = n1;
n1 = n2;
n2 = temp;
}
}
}
3.计算100以内所有的奇数的和,完整代码如下:
namespace _03练习
{
class Program
{
static void Main(string[] args)
{
#region 计算100以内所有的奇数的和
int sum = getOddSum(100);
Console.WriteLine("100以内奇数的和为: "+sum);
Console.ReadKey();
#endregion
}
private static int getOddSum(int num)
{
int sum = 0;
for (int i = 1; i < num; i += 2)
{
sum += i;
}
return sum;
}
}
}
4.请用户输出一个字符串,计算字符串中的字符个数,并打印输出,完整代码如下:
namespace _04练习
{
class Program
{
static void Main(string[] args)
{
#region 请用户输出一个字符串,计算字符串中的字符个数
while (true)
{
Console.WriteLine("请输入一个字符串: ");
string str = Console.ReadLine();
Console.WriteLine("字符个数为: "+str);
Console.ReadKey();
#endregion
}
}
}
5.写一个方法,计算两个数中的最大值,并返回最大值
namespace _05练习
{
class Program
{
static void Main(string[] args)
{
#region 请用户输出一个字符串,计算字符串中的字符个数
Console.WriteLine("请输入第一个数");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("请输入第二个数");
int num2 = int.Parse(Console.ReadLine());
int max = GetMax(num1, num2);
Console.WriteLine("最大值为 {0} " + max);
Console.ReadKey();
#endregion
}
private static int GetMax(int num1, int num2)
{
return num1 > num2 ? num1 : num2;
}
}
}
6.用方法判断给定的整数是不是质数:
namespace _06练习
{
class Program
{
static void Main(string[] args)
{
#region 用方法判断给定的整数是不是质数:
Console.WriteLine("请输入一个整数");
int num = int.Parse(Console.ReadLine());
bool b=Iszhishu(num);
Console.WriteLine(b);
Console.ReadLine();
#endregion
}
//如果不是质数就返回false,是质数就返回true,方法修饰为public,方便求质数的和再次调用
public static bool Iszhishu(int num)
{
for (int i = 2; i < num; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
}
}
7.计算100以内质数的和:
namespace _07练习
{
class Program
{
static void Main(string[] args)
{
#region 计算100以内质数的和
int r = getZhishuSum(100);
Console.WriteLine(r);
Console.ReadKey();
#endregion
}
//如果不是质数就返回false,是质数就返回true
private static bool Iszhishu(int num)
{
for (int i = 2; i < num; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
private static int getZhishuSum(int num)
{
int sum = 0;
for (int i = 2; i < num; i++)
{
if (Iszhishu(i))
{
sum += i;
}
}
return sum;
}
}
}
8.用方法实现:有一个数组;{1,3,5,7,52,55,92,54},找出其中的最大值,并打印输出:
namespace _08练习
{
class Program
{
static void Main(string[] args)
{
#region 用方法判断给定的整数是不是质数
int[] arrInt={1,3,5,7,52,55,92,54};
int max = getArrMax(arrInt);
Console.WriteLine("最大值为{0}",max);
Console.ReadKey();
#endregion
}
private static int getArrMax(int[] arr)
{
int max = arr[0];
for (int i = 1; i < arr.Length; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
return max;
}
}
}
9.计算一组整型数组的平均值:{15,58,6,54,65,4,8,7,2};并且保留两位小数:
namespace _09练习
{
class Program
{
static void Main(string[] args)
{
#region 计算一组整型数组的平均值:{15,58,6,54,65,4,8,7,2};并且保留两位小数
int[] arrInt = {15,58,6,54,65,4,8,7,2};
double avg = getAvg(arrInt);
Console.WriteLine("平均值为:{0}",avg);
Console.ReadKey();
#endregion
}
private static double getAvg(int[] arrInt)
{
int sum = arrInt[0];
for (int i =1; i < arrInt.Length; i++)
{
sum += arrInt[i];
}
return Math.Round(sum/(double)arrInt.Length,2);
}
}
}
10.请通过冒泡排序法对整型数组{5,88,66,1,6,9,15,35}进行排序,按升序排序
namespace _10练习
{
class Program
{
static void Main(string[] args)
{
#region 请通过冒泡排序法对整型数组{5,88,66,1,6,9,15,35}进行排序,按升序排序
int[] arrInt = { 5, 88, 66, 1, 6, 9, 15, 35 };
for (int i = 0; i < arrInt.Length; i++)
{
for (int j = arrInt.Length - 1; j > i; j--)
{
if (arrInt[j] > arrInt[j - i])
{
int temp = arrInt[j];
arrInt[j] = arrInt[j - 1];
arrInt[j - 1] = temp;
}
}
}
for (int n = 0; n < arrInt.Length; n++)
{
Console.WriteLine(arrInt[n]);
}
Console.ReadKey();
}
}
}
11.统计字符串中某个字串出现的位置索引号以及次数:
namespace _11练习
{
class Program
{
static void Main(string[] args)
{
#region 统计字符串中某个字串出现的位置索引号以及次数
string msg ="不知道写什么,那我就瞎写了,傻逼,都死就开始叫 看似简单逗比 是靠的就是可速度快是逗 比";
string s = "逗比";
int index = 0;
int count = 0;
//不等于-1就代表找到了
while ((index = msg.IndexOf(s, index)) != -1)
{
count++;
Console.WriteLine("第{0}次出现【逗比】的索引位置为{1}",count,index);
//找到第一个后,就不用从头开始找了,要从第一次找到的索引值加上字串的长度的位置开始找
index = index + s.Length;
}
Console.WriteLine("[逗比]一共出现了{0}次",count);
Console.ReadKey();
#endregion
}
}
}
12.统计字符串中每个字符出现的次数,用集合来做
namespace _12练习
{
class Program
{
static void Main(string[] args)
{
#region 统计字符串中每个字符出现的次数
string msg = "不知道写什么,那我就瞎写了,傻逼,都死就开始叫 看似简单逗比 是靠的就是可速度快是逗 比";
Dictionary<char,int> dict=new Dictionary<char,int>();
for (int i = 0; i < msg.Length; i++)
{
if (!dict.ContainsKey(msg[i]))
{
dict.Add(msg[i], 1);
}
else
{
dict[msg[i]]++;
}
}
foreach (KeyValuePair<char, int> item in dict)
{
Console.WriteLine("字符{0}出现了{1}次",item.Key,item.Value);
}
Console.ReadKey();
#endregion
}
}
}
13.制作一个控制台小程序。要求:用户可以在控制台录入每个学生的姓名,当用户输入quit(不区分大小写)时,程序停止接受用户的输入,并且显示用户输入的学生的个数以及学生的姓名
namespace _13练习
{
class Program
{
static void Main(string[] args)
{
#region
//添加需求:统计姓沈的人数
int count=0;
string name=string.Empty;//先初始化为空字符串
List<string> list=new List<string>();//用泛型集合来存储
do
{
Console.WriteLine("请输入学生的姓名:");
name=Console.ReadLine();
if(name.IndexOf("沈")==0)
{
count++;
}
list.Add(name);
}while(name.ToLower()!="quit");
//因为最后输出quit的时候,也计算在内了,所以要减掉最后一个元素
list.RemoveAt(list.Count-1);
Console.WriteLine("共输入学生个数{0}次",list.Count);
Console.WriteLine("分别是:");
Console.WriteLine("一共有{0}个姓沈的", count);
//遍历每个学生的姓名(集合中已经存储了每个学生的姓名了)
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
Console.ReadKey();
#endregion
}
}
}
14.将字符串" Hello world,你 好 世界 ! "两端空格去掉,并且将所有的其它空格替换为一个空格,其输出结果是:"Hello world,你 好 世界 !"
namespace _14练习
{
class Program
{
static void Main(string[] args)
{
#region
string msg = " Hello world,你 好 世界 ! ";
string[] words= msg.Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);
msg = string.Join(" ",words);
Console.WriteLine(msg);
Console.ReadKey();
#endregion
}
}
}
15.将普通的日期格式转换中文格式,不考虑"十",例如:2015年9月9号,转换之后为二零一五年九月九号
namespace _14练习
{
class Program
{
static void Main(string[] args)
{
#region
string msg = "2015年9月9号";
msg = CoverToDate(msg);
Console.WriteLine(msg);
Console.ReadKey();
#endregion
}
private static string CoverToDate(string msg)
{
char[] chs= msg.ToCharArray();
for (int i = 0; i < chs.Length; i++)
{
switch (chs[i])
{
case '0':
chs[i] = '零';
break;
case '1':
chs[i] = '一';
break;
case '2':
chs[i] = '二';
break;
case '3':
chs[i] = '三';
break;
case '4':
chs[i] = '四';
break;
case '5':
chs[i] = '五';
break;
case '6':
chs[i] = '六';
break;
case '7':
chs[i] = '七';
break;
case '8':
chs[i] = '八';
break;
case '9':
chs[i] = '九';
break;
default:
break;
}
}
//将字符数组转换为字符串
return new string(chs);
}
}
}

浙公网安备 33010602011771号