C#经典案例实现(求单词在句子出现的次数,字符串倒序输出,用泛型实现两数交换等)
【摘要】 求单词在句子出现的次数 字符串倒序输出 用泛型实现两数交换 求雇员们的总工资 还原 求圆的周长,面积,体积 百钱百鸡 输出年份 求100以内的质数 求单词在句子出现的次数class Cishu { public static void Getnum() { string str="The quick brown fox jumped ...
求单词在句子出现的次数
class Cishu
{
public static void Getnum()
{
string str="The quick brown fox jumped over the lazy dog keeps the doctor away.Can a fox and a dog be friends?";
string key="the";
int count=0;
for(int i = 0; i < str.Length - key.Length; i++)
{
if (str.Substring(i, key.Length) == key)
{
count++;
}
}
Console.WriteLine(key +"在" +str+"中出现" + count+"次");
}
}
字符串倒序输出
class Daoxu
{
public static void Input()
{
string str = "this is string";
char[] ch= str.ToArray();
Array.Reverse(ch);
str = new string(ch);
Console.WriteLine(str);
}
}
用泛型实现两数交换
class Program
{
static void Swap<T>(ref T x,ref T y)
{
T Z = x;
x = y;
y = Z;
}
public static void Main(string[] args)
{
string x = "hello";
string y = "world";
Console.WriteLine("{0},{1}", x, y);
Swap<string>(ref x, ref y);
Console.WriteLine("{0},{1}", x, y);
Console.ReadKey();
}
}
求雇员们的总工资
class Employee
{
public string empName { get; set; }
public float empWage { get; set; }
public static void Main(string[] args)
{
List<Employee> list = new List<Employee>();
list.Add(new Employee() { empName = "李一", empWage = 20 });
list.Add(new Employee() { empName = "张三", empWage = 30 });
list.Add(new Employee() { empName ="王二", empWage= 50 });
list.Add(new Employee() { empName ="赵四", empWage=90});
float sum=0;
list.ForEach(x => { sum += x.empWage; });
Console.WriteLine(sum);
}
}
还原
class Huanyuan
{
public static void Huan()
{
for(int i = 100; i <=999; i++)
{
int j = i * 100 + 95;
if(j%57==0 && j % 67 == 0)
{
Console.WriteLine(j);
}
}
}
}
求圆的周长,面积,体积
class MyMath
{
public static void Perimeter(double R)
{
double c = 2 * Math.PI * R;
Console.WriteLine(c);
}
public static void Area(double R)
{
double s = Math.PI * R * R;
Console.WriteLine(s);
}
public static void Voiume(double R)
{
double v= (3/4)*Math.PI*R*R*R;
Console.WriteLine(v);
}
百钱百鸡
class Maiji
{
public static void Buy()
{
for(int i = 0; i < 20; i++)
{
for (int j = 0; j < 33; j++)
{
if ((100 - i - j)%3==0 && i * 5+ j * 3+ (100 - i - j) / 3 == 100)
{
Console.WriteLine("{0},{1},{2}", i, j, 100 - i - j);
}
}
}
}
}
输出年份
class Nianfen
{
public static void Put()
{
float i = 1.334f;
int n = 123;
string s1 = string.Format("{0:F3}", i);
Console.WriteLine(s1);
string s2 = string.Format("{0:p}", i);
Console.WriteLine(s2);
string s3 = string.Format("{0:00000}", n);
Console.WriteLine(s3);
DateTime dt = new DateTime(2020, 9, 24);
string str = dt.ToString("yyyy年MM月d日");
Console.WriteLine(str);
}
}
求100以内的质数
class Zishu
{
public static void Shu()
{
for(int i = 2; i <= 100; i++)
{
int j = 2;
while (i % j != 0)
{
j++;
}
if (i == j)
{
Console.WriteLine(i);
}
}
}
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)