C#基础练习实例

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

int a = 7, b = 2;
int sum = a + b, dif = a - b, pro = a * b, quo = a / b;
("sum={0},dif={1},pro={2},quo={3}", sum, dif, pro, quo);
;
(mys);
字符串的读取
()用来读取一条字符串。

("请输入你的姓名:");
string name = ();
("你的名字是:" + name);
字符的读取
()用来读取一个字符。

("请输入一个字符:");
int ch = ();;
("c\\windows\\fonts");
("小明说:\"我和他是朋友。

\"");
(@"c\windows\fonts");
string mySentence = "Harvard is ";
mySentence = mySentence + "cccc";
(mySentence);
float pi = ;
("圆周率是" + pi);
int number = 100;
("The number is " + number);
("The number is {0}", number);
int i = 360;
int j = 60;
("{0}+{1}={2}", i, j, i + j);
("{0,6}\n+{1,5}\n------\n{2,6}\n", i, j, i + j);
decimal i1 = ;
decimal j1 = ;
("{0,8:c2}\n+{1,7:c2}\n------\n{2,8:c2}", i1, j1, i1 + j1);
占位符
double pi = ;
("{0:#.00}", pi);
("{0:#.000}", pi);
("{0:#.0000}", pi);
string name = "张三";
int age = 28;
decimal salary = ;
("我叫{0},今年{1}岁了,我的工资是{2}元。

", name, age, salary);
("请输入你的语文成绩?");
int chinese = ());
("请输入你的数学成绩?");
int math = ());
("你的总成绩为:{0}", chinese + math);
int a = 7, b = 2;
int sum = a + b, dif = a - b, pro = a * b, quo = a / b;
("{0}+{1}={2}", a, b, sum);
("{0}-{1}={2}", a, b, dif);
("{0}*{1}={2}", a, b, pro);
("{0}/{1}={2}", a, b, quo);
string name = "张三";
int age = 23;
("{0},我叫" + name, "今年" + age + "岁了");
int age = 18;
int sum = age++ - 10; (4)
("请输入你要计算的天数?");
int days = ());
int week = days / 7;
int mod = days % 7;
("{0}中,共有{1}周零{2}天",days,week,mod);
();
;
}
();
("请输入你买的鸡蛋中有几个坏的?");
int a = ());
if (a < 5)
{
("吃了吧");
}
else
{
("去退货吧");
}
();
("请你输入一个数");
int a = ());
("请你再输入一个数");
int b = ());
if (a % b == 0 || a + b == 100) {
(a);
}
else
{
(b);
}
();
("请输入你的结业考试成绩?");
int a = ());
if (a >= 90)
{
("A");
}
if (90 > a && a >= 80)
{
("B");
}
if (80 > a && a >= 70)
{
("C");
}
if(70>a&&a>=60)
{
("D");
}
if (a < 60)
{
("E");
}
();
int score =95;
("请输入你的结业考试成绩?");
score = ());
if (score>= 90)
{
("A");
}
else if (score >= 80)
{
("B");
}
else if (score >= 70)
{
("C");
}
else if(score>=60)
{
("D");
}
else if (score < 60)
{
("E");
}
();
string password;
("请输入密码?");
password = ();
if (password == "888888")
{
("密码正确!");
}
else
{
("刚才输入的密码不正确,请再次输入密码?"); password = ();
if (password == "888888")
{
("密码正确!");
}
else
{
("密码错误!");
}
();
("请输入用户名?");
string userName = ();
("请输入密码?");
string password = ();
if (userName == "admin" && password == "888888")
("登陆成功!");
else if (userName != "admin" && password != "888888") {
("您输入的用户名和密码都错了!");
}
else if (userName != "admin")
{
("用户名不存在!");
}
else
{
("密码错误!");
}
();
("你现在看的电影可能涉及到血腥暴力,请输入你的年龄?");
int age = ());
if (age >= 18)
{
("你可以观看");
}
else if (age >= 10)
{
("你确定要观看吗?输入yes观看,其他退出");
string input = ();
if (input == "yes")
{
("请观看!");
}
else
{
("你放弃了观看!");
}
}
else
{
("年龄太小,还不能看这种片子");
}
();
("请输入你对李四的评定等级(A—E)?");
string input = ();
decimal salary=5000;
bool flag = false;;
}
else if (userName=="admin" && password != "888888") {
("密码错误!请重新输入.");
}
else if (userName != "admin" && password != "888888") {
("用户名和密码错误!请重新输入.");
}
} while (flag == false);
("恭喜你,登陆成功!");
();
string input;
do
{
("请输入学生的姓名?");
input =();
}while(input !="q");
("输对了!");
();
int input;
bool flag = true;
do
{
try
{ ("请输入一个数字?");
input =());
("{0}的两倍是{1}",input,input*2);
flag=true;
}
catch
{
flag=false;
}
}while(flag);
();
string input;
int number;
("请输入一个数(q结束)?");
input = ();
while (input != "q")
{
number = (input);
("{0}的两倍是{1}", number, number * 2); ("请输入一个数(q结束)?");
input = ();
}
();
int max = 0;
string input = "";
int number;
do
{
try
{
("请输入一个数?");
input = ();
number = (input);
if (number > max)
{
max = number;
}
}
catch
{
if (input != "end")
{
("输入非法数据!程序结束!"); input = "end";
}
}
} while (input != "end");
("最大值为:{0}", max);
();
string userName;
string password;
bool flag = false;
do
{
("请输入用户名?");
userName = ();
("请输入密码?");
password = ();
if (userName == "admin" && password == "888888")
{
flag = true;
}
else if (userName != "admin" && password == "888888") {
("用户名错误!请重新输入.");
}
else if (userName == "admin" && password != "888888") {
("密码错误!请重新输入.");
}
else if (userName != "admin" && password != "888888") {
("用户名和密码错误!请重新输入.");
}
} while (flag == false);
("恭喜你,登陆成功!");
();
string userName;
string password;
bool flag = false;
do
{
flag=false;
("请输入用户名?");
userName = ();
("请输入密码?");
password = ();
if (userName != "admin" || password != "888888") {
("用户名或密码错误!请重新输入!");
flag=true;
}
}while(flag);
();
int i = 0;
while (i < 100)
{
("下次我一定细心!");
i++;
}
();
for (int i = 0; i < 100; i++)
{
("下次我一定要细心!"+i);
}
();
int sum = 0;
for (int i = 1; i <=100; i++)
{
if (i % 2 == 0)
{
sum += i;;
("我叫{0},今年我{1}岁了,我是{2}性",
, , ;
Person secPerson;
= "lisi";
= 20;
= Gender.男;
("我叫{0},今年我{1}岁了,我是{2}性",
, , ;
();
}
{
int number = 100;
Test(ref number);
(number);
();
}
static void Test(ref int a)
{
int b = a;
a = 500;
}
{
string input = "";
int number = 0;
bool result;
do
{
("请输入一个正整数?");
input = ();
if (input == "q")
{
("程序结束!");
break;
}
try
{
number = (input);
result = ZhiShu(number);
if(result==true)
{
("{0}是质数", number);
}
else
{
("{0}不是质数", number);
}
}
catch
{
("输入的数据有误,请重新输入!");
continue;
}
}
while (true);
();
}
public static bool ZhiShu(int number)
{
for(int i=2;i<number;i++)
if (number % i == 0)
{
return false;
}
return true;
}
{
string[] names = { "张三", "李四", "王五", "赵六" };
string str = ConString(names);
(str);
();
}
public static string ConString(string[] names)
{
string result = "";
for (int i = 0; i < ; i++)
{
if (i == - 1)
{
result += names[i];
}
else
{
result += names[i] + "|";
}
}
return result;
}
class Program
{
static void PrintArr(int ArrLength)
{
int[] arr = new int[ArrLength];
for (int i = 0; i < ; i++)
arr[i] = i;
("'s value");
for (int i = 0; i < ; i++)
("arr[{0}]={1}", i, arr[i]);
}
static void Main(string[] args)
{
int i = 1;
while (i > 0)
{
("Please enter the arry's length:");
i = ());
PrintArr(i);
}
;
("我叫{0},我今年{1}岁了,我是{2}性",
, , ;
Person secPerpon;
= "李四";
= 26;
= Gender.男;
("我叫{0},我今年{1}岁了,我是{2}性",
, , ;
();
ShowUI();
int[]scores={6,2,9,8,3,7};
for(int i=0;i<;i++);
(sex);
();
Gender sex;
sex = Gender.男;
switch (sex)
{
case Gender.男:
("男性");
break;
case Gender.女:
("女性");
break;
}
();
Gender sex;
sex = Gender.男;
("请输入你的性别?");
string s = ();
try
{
sex = (Gender)(typeof(Gender), s));;
}
{
("Now time is{0}",MyMethod());,'|');
foreach (string item in strs)
{
(item);
}
string s3 = "aa,bb,ccc,,12,3";
string[] strs = (',');
foreach (string item in strs)
{
(item);
}
string s1 = "aa,bb,ccc,,12,3";
string[] strs = (new char[]{','},;
foreach (string item in strs)
{
(item);
}
string s1 = "我是张三我是李四我是王五";
string[] strs = (new string []{"我是"},;
foreach (string item in strs)
{
(item);
}
string s = "李时珍同志是一个好同志,李时珍是一个好医生,向李时珍同志学习。

"; s = ("李时珍", "李素丽");
(s);
string s = "";
string域名 = (7); string email = ();
int atIndex = ('@');
string username = (0, atIndex);
string域名 = (atIndex + 1);
(username);
(域名);
string value = GetConfigValue(@"d:\","端口");
(value);
();
}
static string GetConfigValue(string filename, string itemName)
{
string[] lines = ;
foreach (string line in lines)
{
string[] strs = ('=');
string name = strs[0];
string value = strs[1];
if () == itemName)
{
return value;
}
}
return"没找到,不存在!";//如果不加这一行,那么程序就存在没有返回值的情况
//机器人(2)
("你好,我是机器人!");
while (true)
{
string str = ();
//天气北京
if ("天气"))
{
string city = (2);
("{0}的天气晴朗",city);
}
else if ("你") && ("名字") || ("姓名")))
{
("不要崇拜哥,哥只是个传说。

");
}
else if ("88") || ("再见") || ("bye"))
{
("撒有那拉!");
();
return;//return表示终止程序(Main),即退出程序
}
}
int age = 20;
IncAge(age);
//IncAge(ref age);
IncAge(out age);
(age);
();
}
static void IncAge(int age)//函数参数默认是值传递,也就是“复制一份” {
age++;
}
//static void IncAge(ref int age)//ref应用场景是内部对外部的值进行改变
//{
// age++;
//}
static void IncAge(out int age)//out一般用在函数有多个返回值的场所
{
age = 30;//内部为外部赋值
}
string str = ();
int i;
if (str, out i))
{
("转换成功,{0}", i);
}
else
{
("数据错误!");
}
int i1 = 10;
int i2 = 20;
swap(ref i1, ref i2);
("i1={0},i2={1}",i1,i2);
();
}
static void swap(ref int i1, ref int i2) {
int temp = i1;
i1 = i2;
i2 = temp;
}。

相关文档
最新文档