C# 基础知识小练习总结(全注释)
判断成绩等级,90~100分 为 优,80~89分 为 良,70~79分 为 中, 60~69分 为 及格,0~59分 为 不及格。
设计界面:
代码:
using System;
using System.Windows.Forms;
namespace 判断
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int score;//声明整数变量
if (int.TryParse(textBox1.Text, out score))//判断是否可以转换为数字(分数)
{
if (score < 101 && score >= 0)//判断分数 是否小于100 大于0
{
Judge(score);//调用方法
}
else
{
MessageBox.Show("成绩错误。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);//提示
}
}
else
{
MessageBox.Show("成绩错误。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);//提示
}
}
void Judge(int score)//方法
{
string str;//字符串
switch (score / 10)//整除
{
case 10:
case 9://判断
str = "优";//赋值
break;
case 8:
str = "良";
break;
case 7:
str = "中";
break;
case 6:
str = "及格";
break;
default:
str = "不及格";
break;
}
textBox2.Text = str;//输出
}
}
}输出结果:
输出九九乘法表:
using System;
namespace 九九乘法表
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 9; i++)//循环
{
for (int j = 1; j <= 9; j++)//循环 如果 将 j <= 9 改为 j <= i 则输出结果2
{
Console.Write("{0}x{1}={2}\t",i,j,i*j);//输出
}
Console.WriteLine();//换行
}
Console.ReadKey();//按任意键继续
}
}
}输出结果1:
将 j <= 9 改为 j <= i 输出结果2:
定义长度50的数组,随机给数组赋值,并可以让用户输入一个数组n,按一行n个数输出数组。
using System;
namespace 输出指定数行
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[50];//声明数组
Random r = new Random();//随机
for (int i = 0; i < 50; i++)//循环赋值
{
array[i] = r.Next(0,10);//将0~9随机数赋值
}
Console.WriteLine("请输入一个数字:");//提示
int n = int.Parse(Console.ReadLine());//接收输入的数字 并转化为数字
for (int i = 0; i < array.Length; i++)//循环
{
Console.Write(array[i]+"\t");//输出
if ((i + 1) % n == 0)//判断 i+1 求余 n 为0
{
Console.WriteLine();//换行
}
}
Console.ReadKey();//按任意键继续
}
}
}输出结果:
编写一个函数,接收一个字符串,把用户输入的字符串中的第一个字母转换成小写然后返回。
using System;
namespace 将首字母转换为小写
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个字符串:");//提示
string str = Console.ReadLine();//接收用户输入字符串
str = str.Substring(0, 1).ToLower() + str.Substring(1);//将字符串首字母 转换为小写 并+上后面字符串赋值给str
//ToLower() 将字符串转换为 小写,ToUpper()将字符串转换为 大写。
Console.WriteLine(str);//输出
Console.ReadKey();
}
}
}输出结果:
声明两个变量:int n1 = 10,n2=20;要求将两个变量交换,不使用第三个变量。
using System;
namespace 两个变量交换
{
class Program
{
static void Main(string[] args)
{
int n1 = 10, n2 = 20;//声明
Console.WriteLine("当前n1的值为{0},n2的值为{1}", n1, n2);//输出
n1 = n1 - n2;// n1 = 10-20 = -10
n2 = n2 + n1;// n2 = 20+(-10) = 10
n1 = n2 - n1;// n1 = 10-(-10) = 20
Console.WriteLine("转换后n1的值为{0},n2的值为{1}",n1,n2);//输出
Console.ReadKey();
}
}
}输出结果:
用方法来实现:将两个变量交换.使用ref(引用传递)
using System;
namespace ref两值交换
{
class Program
{
static void Main(string[] args)
{
int n1 = 10, n2 = 20;//声明变量
Console.WriteLine("当前n1的值为{0},n2的值为{1}", n1, n2);//输出
GetNum(ref n1,ref n2);//调用方法,ref 引用传值
Console.WriteLine("转换后n1的值为{0},n2的值为{1}", n1, n2);//输出
Console.ReadKey();//按任意键继续
}
static void GetNum(ref int n1,ref int n2)//方法
{
int temp;//声明变量
temp = n1;//将n1赋值给temp
n1 = n2;//将n2赋值给n1
n2 = temp;//将temp赋值给n2
}
}
}输出结果:
请用户输入一个字符串,计算字符串中的字符个数,并输出。
using System;
namespace 计算字符串字符个数
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个字符串:");//提示
string str = Console.ReadLine();//获取用户输入的字符串
int num = str.Length;//获取字符串中字符个数
Console.WriteLine(str+"共包含{0}个字符.",num);//输出
Console.ReadKey();//按任意键继续
}
}
}输出结果:
用方法实现:1、计算两个数的最大数。
using System;
namespace 计算两个数最大数
{
class Program
{
static void Main(string[] args)
{
int n1 = 10, n2 = 20;//声明变量
GetMaxNum(n1, n2);//调用方法
Console.ReadKey();//按任意键继续
}
static void GetMaxNum(int n1,int n2)//方法
{
if (n1 > n2)//判断
{
Console.WriteLine("n1为最大数");//输出
}
else
{
Console.WriteLine("n2为最大数");//输出
}
}
}
}1、输出结果:
2、计算多个数之间的最大数。
using System;
namespace 计算多个数之间最大数
{
class Program
{
static void Main(string[] args)
{
int[] num = new int[] { 2, 6, 8, 9, 7, 3, 1, 4, 5 };//声明数组
GetMaxNum(num);//调用方法
Console.ReadKey();//按任意键继续
}
static void GetMaxNum(params int[] num)//方法
{
int max = 0;//声明变量
for (int i = 0; i < num.Length; i++)//循环
{
if (max < num[i])//判断数组元素是否大于max
{
max = num[i];//赋值
}
}
Console.WriteLine("数组中最大数为{0}.", max);//输出
}
}
}2、输出结果:
用方法实现:计算1~100之间的所以整数的和。
using System;
namespace 计算1_100之间的所以整数的和
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("1~100之间所有数和为: "+GetSum());//调用输出
Console.ReadKey();
}
static int GetSum()//方法
{
int sum = 0;//声明变量
for (int i = 0; i < 101; i++)//循环
{
sum += i;//相加 sum = sum + i;
}
return sum;//返回结果
}
}
}输出结果:
用方法实现:计算1~100之间所有奇数的和。
using System;
namespace 计算1_100之间所有奇数的和
{
class Program
{
static void Main(string[] args)
{
GetOddNumber();//调用方法
Console.ReadKey();//按任意键继续
}
static void GetOddNumber()//方法
{
int sum = 0;
for (int i = 0; i < 101; i++)//循环
{
if (i % 2 != 0)//不能被2 整除的数为奇数
{
sum += i;//相加
}
}
Console.WriteLine("1~100之间所有奇数和:{0}", sum);//输出
}
}
}输出结果:
用方法实现:判断一个给定的整数是否为“质数”。
using System;
namespace 判断质数
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("请输入一个数:");//提示
int num = Convert.ToInt32(Console.ReadLine());//接收
GetPrimeNum(num);//调用
}
}
static void GetPrimeNum(int num)
{
if (num < 2)//判断 数字是否小于2
{
Console.WriteLine(num + " 不是质数。");//提示
}
else
{
for (int i = 2; i < num; i++)//循环
{
if (num % i == 0)//判断num 与 i 求余 为0
{
Console.WriteLine(num + " 不是质数。");//提示
}
}
Console.WriteLine(num + " 是质数。");//提示
}
}
}
}输出结果:
用方法实现:有一个字符串数组:{"马龙","迈克尔乔丹","雷吉米勒","蒂姆邓肯","科比布莱恩特"},请输出最长的字符串。
using System;
namespace 输出最长的字符串
{
class Program
{
static void Main(string[] args)
{
string[] str = new string[] { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };//数组
GetLenStr(str);//调用方法
Console.ReadKey();//按任意键继续
}
static void GetLenStr(string[] str)//方法
{
string s = "";//变量
for (int i = 0; i < str.Length; i++)//循环
{
if (s.Length < str[i].Length)//判断字符串长度
{
s = str[i];//赋值
}
}
Console.WriteLine("最长为:{0}",s);//输出
}
}
}输出结果:
用方法实现:计算出一个整型数组的平均值。{1,3,5,7,93,33,4,4,6,8,10} 要求:计算结果如果有小数,则显示小数点后两位(四舍五入)。
using System;
namespace 平均值
{
class Program
{
static void Main(string[] args)
{
int[] num = new int[] { 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 };//数组
double avg = GetAvg(num);//调用方法
avg = Convert.ToDouble(avg.ToString("0.00"));//将avg转换为 字符串,保留两位小数,在转换为double类型
Console.WriteLine(avg);//输出
Console.ReadKey();
}
static double GetAvg(int[] num)//方法
{
double sum = 0;
for (int i = 0; i < num.Length; i++)//循环
{
sum += num[i];//总和
}
return (sum / num.Length);//平均数
}
}
}输出结果:
通过冒泡排序法对整数数组{1,3,5,7,90,2,4,6,8,10}实现升序排列。
using System;
namespace 冒泡排序
{
class Program
{
static void Main(string[] args)
{
int[] num = new int[] { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };//数组
for (int i = 0; i < num.Length-1; i++)//外层循环控制排序趟数
{
for (int j = 0; j < num.Length-1-i; j++)//内层循环控制每一趟排序多少次
{
if (num[j] > num[j+1])//判断
{
int temp;
temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
//交换两值
}
}
}
for (int i = 0; i < num.Length; i++)//循环
{
Console.WriteLine(num[i]);//输出
}
Console.ReadKey();
}
}
}输出结果:
有如下字符串:【"患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"】。需求:①请统计出该字符中“咳嗽”二字的出现次数,以及每次“咳嗽”出现的索引位置。②扩展(*):统计出每个字符的出现次数。
using System;
namespace 统计
{
class Program
{
static void Main(string[] args)
{
string str = "患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”";//字符串
int index = str.IndexOf("咳嗽");//搜索第一个 “咳嗽” 在的位置
int i = 1;//用于记录 找到 “咳嗽” 的次数
Console.WriteLine("第1次出现咳嗽的位置:{0}", index);//输出
while (index != -1)//判断index 不等于-1
{
i++;//自增1
index= str.IndexOf("咳嗽", index+1);//查找“咳嗽”位置
if (index==-1)//判断是否 等于-1
{
break;//退出循环
}
Console.WriteLine("第{0}次出现咳嗽的位置:{1}",i, index);//输出
}
Console.ReadKey();
}
}
}输出结果:
将字符串" hello world,你 好 世界 ! "两端空格去掉,并且将其中的所有其他空格都替换成一个空格,输出结果为:"hello world,你 好 世界 !"。
using System;
namespace 去空格
{
class Program
{
static void Main(string[] args)
{
string str = " hello world,你 好 世界 ! ";//字符串
str.Trim();//移除字符串前后空格
string[] strs = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//分割
str= string.Join(" ", strs);//串联字符串数组的所有元素,其中在每个元素之间使用指定的分隔符。
Console.WriteLine(str);//输出
Console.ReadKey();//按任意键继续
}
}
}输出结果:
