for循环
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _01_for循环
{
class Program
{
static void Main(string[] args)
{
//break
//int i = 0;
//while (i < 10)
//{
// Console.WriteLine("Hello World");
// i++;
//}
//for (int i = 0; i < 10; i++)
//{
// Console.WriteLine("Hello World");
//}
//使用for循环求1-100之间所有整数的和
int sum = 0;
for (int i = 1; i <= 100; i+=2)
{
sum += i;
}
Console.WriteLine(sum);
Console.ReadKey();
}
}
}
for循环的正序和倒序输出
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _02_for循环的正序和倒序输出
{
class Program
{
static void Main(string[] args)
{
//1-10
//for (int i = 1; i <=10 ; i++)
//{
// Console.WriteLine(i);
//}
//Console.ReadKey();
10-1
//for (int i = 10; i >=1; i--)
//{
// Console.WriteLine(i);
//}
//for (int i = 10; i >= 1; i--)
//{
// Console.WriteLine(i);
//}
Console.ReadKey();
}
}
}
水仙花数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03_水仙花数
{
class Program
{
static void Main(string[] args)
{
//水仙花数:100-999
//这个百位数字 百位的立方+十位的立方+个位的立方==当前这个百位数字
//153 1+125+27 153 i
//百位:153/100
//十位:153%100/10
//个位:153%10
for (int i = 100; i <= 999; i++)
{
int bai = i / 100;
int shi = i % 100 / 10;
int ge = i % 10;
if (bai * bai * bai + shi * shi * shi + ge * ge * ge == i)
{
Console.WriteLine(i);
}
}
Console.ReadKey();
}
}
}
for循环的嵌套
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04_for循环的嵌套
{
class Program
{
static void Main(string[] args)
{
// int count = 0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
//count++;
Console.WriteLine("外面循环了{0}次,里面循环了{1}次",i,j);
}
}
Console.ReadKey();
}
}
}
5乘法口诀表
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _05乘法口诀表
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <=i ; j++)
{
Console.Write("{0}*{1}={2}\t", i, j, i * j);
}
Console.WriteLine();//换行
}
Console.ReadKey();
}
}
}
for循环练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _06for循环练习
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个数字");
int num = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i <=num ; i++)
{
Console.WriteLine("{0}+{1}={2}", i, num - i, num);
}
Console.ReadKey();
}
}
}
break
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _07break
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Console.WriteLine("Hello World");
break;
}
}
Console.ReadKey();
}
}
}
类型转换
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _08类型转换
{
class Program
{
static void Main(string[] args)
{
//string s = "123";
int n = Convert.ToInt32(s);
//int n = int.Parse(s);
//Console.WriteLine(n);
//Console.ReadKey();
int result;// = 100;
bool b = int.TryParse("12abc3", out result);
Console.WriteLine(result);
Console.WriteLine(b);
Console.ReadKey();
//函数 方法 找个人帮你做一件事情 参数 返回值
}
}
}
3个练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _09_3个练习
{
class Program
{
static void Main(string[] args)
{
//练习1:输入班级人数 并计算平均年龄,
//如果录入的数据出现负数或大于100的数,立即停止输入并报错.
//只要输入有误,就要求一直重新输入
#region 练习1
//int count=0;
//while (true)
//{
// Console.WriteLine("请输入班级人数");
// try
// {
// count = Convert.ToInt32(Console.ReadLine());
// break;//如果count转换成功,则没有必要继续循环
// }
// catch//输入有误
// {
// Console.WriteLine("输入有误!请重新输入");
// }
//}
//bool b = true;
//int sum=0;
//for (int i = 0; i < count; i++)
//{
// Console.WriteLine("请输入第{0}个人的年龄", i + 1);
// try
// {
// int age = Convert.ToInt32(Console.ReadLine());
// if (age >= 0 && age <= 100)
// {
// sum += age;
// }
// else
// {
// b = false;
// Console.WriteLine("输入的年龄不再正确范围内!!程序退出!!");
// break;
// }
// }
// catch//输入的不是数字
// {
// i--;
// }
//}
//if (b)
//{
// Console.WriteLine("{0}个人的班级平均年龄是{1}", count, sum / count);
//}
//Console.ReadKey();
#endregion
//练习2:在while中用break实现要求用户一直输入用户名和密码,
//只要不是admin、88888就一直提示要求重新输入,如果正确则提登录成功.
//while (true)
//{
// Console.WriteLine("请输入用户名");
// string name = Console.ReadLine();
// Console.WriteLine("请输入密码");
// string pwd = Console.ReadLine();
// if (name == "admin" && pwd == "88888")
// {
// Console.WriteLine("登陆成功");
// break;
// }
//}
//1~100之间的整数相加,得到累加值大于20的当前数(比如:1+2+3+4+5+6=21)结果6
int sum = 0;
for (int i = 1; i <= 100; i++)
{
sum += i;
if (sum >= 20)
{
Console.WriteLine(i);
break;
}
}
Console.ReadKey();
}
}
}
continue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _10continue
{
class Program
{
static void Main(string[] args)
{
//int i = 0;
//while (i < 10)
//{
// // break;
// continue;
// Console.WriteLine("Hello World");
//}
//Console.ReadKey();
//练习1:用 while continue实现计算1到100(含)之间的除了能被7整除之外所有整数的和。
//int i = 1;
//int sum = 0;
//while (i<=100)
//{
// if (i % 7 == 0)
// {
// i++;
// continue;
// }
// sum += i;
// i++;
//}
//Console.WriteLine(sum);
//Console.ReadKey();
//打印1-100之间所有的质数
//质数:只能被1和自身整除的数字 叫做质数
//判断一个整数是否是质数:让这个整数从2开始除,除到自己的前一位
for (int i = 2; i <= 100; i++)
{
bool b = true;
for (int j = 2; j < i; j++)
{
if (i % j == 0)//不是质数
{
b = false;
break;
}
}
if (b)
{
Console.WriteLine(i);
}
}
Console.ReadKey();
}
}
}
三元表达式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _11三元表达式
{
class Program
{
static void Main(string[] args)
{
//提示用户输入两个数字 我们输出比较大的那个数字
//Console.WriteLine("请输入第一个数字");
//int numberOne = Convert.ToInt32(Console.ReadLine());
//Console.WriteLine("请输入第二个数字");
//int numberTwo = Convert.ToInt32(Console.ReadLine());
表达式1?表达式2:表达式3;
//int max = numberOne > numberTwo ? numberOne : numberTwo;
//Console.WriteLine(max);
//Console.ReadKey();
//if (numberOne > numberTwo)
//{
// Console.WriteLine(numberOne);
//}
//else
//{
// Console.WriteLine(numberTwo);
//}
//Console.ReadKey();
Console.WriteLine("请输入一个姓名");
string name = Console.ReadLine();
//if (name == "老赵")
//{
// Console.WriteLine("孙中山转世");
//}
//else
//{
// Console.WriteLine("山炮一个");
//}
string result = name == "老赵" ? "孙中山转世" : "山炮一个";
Console.WriteLine(result);
Console.ReadKey();
}
}
}
1.产生随机数
Random r=new Random(); //创建一个产生随机数的对象
int rNumber=r.Next(1,10);调用方法,范围
Random r = new Random();
while (true)
{
int rNumber = r.Next(1, 7);
Console.WriteLine("请输入姓名,我们将显示这人人上辈子的职业");
string name = Console.ReadLine();
switch (rNumber)
{
case 1: Console.WriteLine("上辈子是个太监", name);
break;
case 2: Console.WriteLine("上辈子是个zzzz", name);
break;
case 3: Console.WriteLine("上辈子是个狗", name);
break;
case 4: Console.WriteLine("上辈子是个人", name);
break;
case 5: Console.WriteLine("上辈子是个帅狗", name);
break;
case 6: Console.WriteLine("上辈子是个李世民", name);
break;
}
Console.ReadKey();
随机数的时间种子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _20随机数的时间种子
{
class Program
{
static void Main(string[] args)
{
Random r = new Random();
for (int i = 0; i < 100; i++)
{
int rNumber = r.Next(1, 100);
Console.WriteLine(rNumber);
}
Console.ReadKey();
}
}
}
2.常量 const 不能被重新赋值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _14常量
{
class Program
{
static void Main(string[] args)
{
//const int number = 10;
//number = 30;
// const double rate = 0.8;
//大学管理系统 性别 3个程序员
//爷们 女人 男 女 male famele
//规范程序员的开发过程
}
}
}
3枚举
[public] enum 枚举名
{
值1,
值2,
值3,
........
}
public:访问修饰符。访问的权限,公开的,公共的,谁都能访问。
enum:表示声明枚举的关键字
枚举名:必须要Pascal命名规范。每个单词的首字母都要大写。
不能在Main函数写枚举 写在namespace里
枚举类型和int类型互相兼容,所以两种类型可以通过强制类型转换的语法互相转换。
枚举类型的值默认是从0开始的。底下的与上面的是加一的关系
不管是将字符串类型的数字转换成枚举类型还是将int类型转换成枚举类型,结果都不会抛异常。
但是,如果要转换的字符串,在枚举中,并没有所对应的值,这个时候会抛异常。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _15枚举
{
public enum Gender
{
男,
女
}
public enum Sesons
{
春,
夏,
秋,
冬
}
class Program
{
static void Main(string[] args)
{
//int number=10;
Gender gender = Gender.女;
Sesons seson = Sesons.春;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _17枚举类型和string类型_int类型之间的转换
{
public enum QQState
{
OnLine=1,
OffLine,
Leave,
Busy,
QMe
}
class Program
{
static void Main(string[] args)
{
#region 将枚举类型强制转换为int类型
//枚举类型和int类型互相兼容
//int n1 = (int)QQState.OnLine;
//Console.WriteLine(n1);
//Console.WriteLine((int)QQState.OffLine);
//Console.WriteLine((int)QQState.Leave);
//Console.WriteLine((int)QQState.Busy);
//Console.WriteLine((int)QQState.QMe);
//Console.ReadKey();
#endregion
#region 将int类型强制转换为枚举类型
//int n1 = 10;
//QQState s1 = (QQState)n1;
//Console.WriteLine(s1);
//Console.ReadKey();
#endregion
#region 将枚举类型转换为字符串类型 调用ToString()
//QQState s = QQState.OnLine;
//Console.WriteLine(s.ToString());
//Console.ReadKey();
#endregion
#region 将字符串类型转换为枚举类型
//QQState s1 = (QQState)Enum.Parse(typeof(QQState), " OffLine");
//Console.WriteLine(s1);
//Console.ReadKey();
#endregion
Console.WriteLine("请选择您的qq在线状态 1--OnLine,2--OffLine 3--Leave 4--Busy 5--QMe ");
string input = Console.ReadLine();//1-5
switch (input)
{
case "1": QQState s1 = (QQState)Enum.Parse(typeof(QQState), input);
Console.WriteLine("您选择在线状态是{0}",s1);
break;
case "2": QQState s2 = (QQState)Enum.Parse(typeof(QQState), input);
Console.WriteLine("您选择在线状态是{0}",s2);
break;
case "3": QQState s3 = (QQState)Enum.Parse(typeof(QQState), input);
Console.WriteLine("您选择在线状态是{0}",s3);
break;
case "4": QQState s4 = (QQState)Enum.Parse(typeof(QQState), input);
Console.WriteLine("您选择在线状态是{0}", s4);
break;
case "5": QQState s5 = (QQState)Enum.Parse(typeof(QQState), input);
Console.WriteLine("您选择在线状态是{0}", s5);
break;
}
Console.ReadKey();
}
}
}
4.结构: 一次性声明多个不同类型的变量
[public] struct 结构名
{
结构成员;
}
字段跟变量有一个本质的区别:字段可以存储多个值,而变量只能存储一个值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18结构类型
{
public struct Person
{
public string _name;
public int _age;
public Gender _gender;
}
public enum Gender
{
男,
女
}
class Program
{
static void Main(string[] args)
{
//首先声明一个结构类型的变量
Person zsPerson;
zsPerson._name = "张三";
zsPerson._age = 19;
zsPerson._gender = Gender.男;
Person lsPerson;
lsPerson._name = "李四";
lsPerson._age = 20;
lsPerson._gender = Gender.女;
大学管理系统 姓名 性别 年龄
//string name1 = "张三";
//char gender1 = '男';
//int age1 = 15;
}
}
}
枚举练习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18结构练习
{
public struct MyColor
{
public int _red;
public int _green;
public int _blue;
}
class Program
{
static void Main(string[] args)
{
MyColor color;
color._red = 255;
color._green = 0;
color._blue = 0;
//int
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _19结构练习
{
public struct Person
{
public string _name;
public int _age;
public Gender _gender;
}
public enum Gender
{
男,
女
}
class Program
{
static void Main(string[] args)
{
Person zsPerson;
zsPerson._name = "张三";
zsPerson._age = 18;
zsPerson._gender = Gender.男;
Person xlPerson;
xlPerson._name = "小兰";
xlPerson._age = 16;
xlPerson._gender = Gender.女;
}
}
}
枚举规范程序员开发
结构声明不同类型的变量