using System.Formats.Asn1;
namespace MyFirstConsoleApp
{
internal class Program
{
static void Main(string[] args)
{
TryAnIF();
TryAnIfElse();
TrySomeLoops();
}
///
/// 要点
///
///
/***
*不要忘记所有语句都要以一个分号结束
*行首使用两个斜线为代码增加注释
*声明变量时要提供一个类型,后面是一个名字
*大多数情况下,可以有额外空白符
*if/else、while、do和for都要测试条件
***/
private static void TryAnIF()
{
int someValue = 4;
string name = “Bobbo Jr.”;
if ((someValue == 3) && (name == “Joe”))
{
Console.WriteLine(“x is 3 and the name is Joe”);
}
Console.WriteLine(“this line runs no matter what”);
}
private static void TryAnIfElse()
{
int x = 5;
if (x == 10)
{
Console.WriteLine("x must be 10");
}
else
{
Console.WriteLine("x isn't 10");
}
}
private static void TrySomeLoops()
{
int count = 0;
while (count < 10)
{
count = count + 1;
}
for (int i = 0; i < 5; i++)
{
count = count - 1;
}
Console.WriteLine("The answer is " + count);
}
private static int area;
private static int height;
private static int width;
static void Main(string[] args)
{
OperatorExamples();
}
private static void OperatorExamples()
{
Console.WriteLine(area);
while (area < 20)
{
height++;
area = width * height;
}
do
{
width--;
area = width * height;
} while (area > 25);
//This statement declares a variable and sets it to 3
int width = 3;
//The == operator increments a variable (adds 1 to 3)
width++;
//Declare two more int variables to hold numbers and
//use the + and * operators to add and multiple values
int height = 2 + 4;
int area = width * height;
Console.WriteLine(area);
//The next two statements declare string variables
//and use + to concatenate them (join them together)
string result = "The area";
result = result + " is " + area;
Console.WriteLine(result);
//A Boolean variable is either true or false
bool truthValue = true;
Console.WriteLine(truthValue);
//“if”语句做决策
int someValue = 10;
string message = "";
if (someValue == 24)
{
message = "Yes,it' 24!";
}
//if/else语句在条件不为真时也会做些事情
if (someValue == 24)
{
message = "Yes,it' 24!";
}
else
{
message = "The value wasn't 24.";
}
//while循环就是当条件为true时循环执行语句
int x = 10;
while (x > 5)
{
}
//do/while循环先运行语句再检查条件
do
{
} while (x > 5);
//for循环在每次循环后运行一个语句
for (int i = 0; i < 8; i = 1 + 2)
{
}
LOOP#1
int count = 5;
while (count > 0)
{
count = count * 3;
count = count * -1;
}
LOOP #2
int j = 2;
for (int i = 1; i < 100; i = i * 2)
{
j = j - 1;
while (j < 25) //执行7次
{
//下一个语句会执行多少次?
j = j + 5; //执行6次
}
}
//LOOP #3
int p = 2;
for (int q = 2; q < 32; q = q * 2)
{
while (p < q) //执行10次
{
p = p * 2;//执行3次
}
q = p - q;
}
LOOP #4
int i = 0;
int count = 2;
while (i == 0)//死循环
{
count = count * 3;
count = count * -1;
}
}
}
}
}