unity3D-游戏/AR/VR在线就业班 C#入门If 语句学习笔记
http://edu.csdn.NET/lecturer/107
一、if(条件表达式){语句1}
using System;
namespace Lesson11
{
class MainClass
{
public static void Main (string[] args)
{
//顺序结构
// int a =4;
// int b = a * 2 + 16;
// Console.WriteLine (b);
int i = 0;
i = int.Parse(Console.ReadLine ());
// 条件表达式值是true,就会执行后面代码块的代码,否则不会执行
if(i %2==0)
{
Console.WriteLine ("输入的数是偶数");
}
Console.WriteLine ("[Over]x");
}
}
}
二、if(条件表达式){语句1;} else{语句2;}
using System;
namespace Lesson11
{
class MainClass
{
public static void Main (string[] args)
{
//顺序结构
// int a =4;
// int b = a * 2 + 16;
// Console.WriteLine (b);
int i = 0;
i = int.Parse(Console.ReadLine ());
// 条件表达式值是true,就会执行后面代码块的代码,否则不会执行
//一、if(条件表达式){语句1}
// if(i %2==0)
// {
// Console.WriteLine ("输入的数是偶数");
// }
// Console.WriteLine ("[Over]");
//二、if(条件表达式){语句1;} else{语句2;}
if (i % 2 == 0) {
Console.WriteLine ("输入的数是偶数");
} else {
Console.WriteLine ("奇数");
}
Console.WriteLine ("[Over]");
}
}
}
三、if(条件表达式1){语句1;} else if(条件表达式2){语句2;}else{语句3}
//三、if(条件表达式1){语句1;} else if(条件表达式2){语句2;}else{语句3}
int i = int.Parse(Console.ReadLine ());
if (i < 0) {
Console.WriteLine ("负数");
} else if (i >= 0 && i < 10) {
Console.WriteLine ("一位数");
} else if (i >= 10 && i < 100) {
Console.WriteLine ("两位数");
} else {
Console.WriteLine ("你输入的数太大了,崩~");
}
int money = int.Parse (Console.ReadLine ());
string str = "";
if (money >= 100) {
str = "买买买~";
} else {
str = "回家吃土";
}
Console.WriteLine (str);
四、问号表达式 条件表达式?表达式1:表达式2;
//问号表达
int money = int.Parse (Console.ReadLine ());
string str = "";
str =(money>=100)?"买买买~":"回家吃土";
Console.WriteLine (str);