最简单的语句:方法调用语句及赋值语句
后面有个分号
如:
System.Console.Write("Hello World");
b=a>0?a:-a;
s=TextBox1.Text;
d=int.Parse(s);
注意:没有表达式语句一说
2+3;不能称为一个语句;
if(条件表达式)
语句块
else
语句块
LeapYear.cs
if((year%4==0&&year%100!=0)||(year%400==0))
Console.WriteLine(year+"是润年");
else
Console.WriteLine(year+"不是闰年");
小技巧
注意书写格式,特别是缩进;
将后面的花括号去掉,再重新输一下,就会自动排版;
也可以按Ctrl+E+D(格式文档)
或 Ctrl+E+F(格式化选中部分)
屏幕保护源代码:
Winform窗体无边框属性 Font->FormBorderStyle改位None,WindowState 改为Maximized;
添加timer label 控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 屏幕保护
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int deltX = 10;
int deltY = 8;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Application.Exit();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.label1.Left += deltX;
this.label1.Top += deltY;
if (this.label1.Top < 0 ||
this.label1.Top + this.label1.Height > this.Height)
deltY = -deltY;
if (this.label1.Left < 0 ||
this.label1.Left + this.label1.Width > this.Width)
deltX = -deltX;
}
}
}
switch (exp)
{
case const1:
statement1;
break;
case const2:
statement2;
brek;
.....
case constN:
statementN;
break;
default:
statement_dafault;
break;
}
Switch 语句与C++不同之处
① 变量处理整型、枚举型、还可以用字符串;
② 不能随便贯穿,必须有break;(除非几个case连起来,中间没有别的语句);
出题判分:button、label、textBox、listBox控件组成
源代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 自动出题并判分
{
public partial class Form1 : Form
{
int a, b;
string op;
int result;
Random rnd = new Random();
///
/// 出题
///
///
///
private void button1_Click(object sender, EventArgs e)
{
a = rnd.Next(9) + 1;
b = rnd.Next(9) + 1;
int c = rnd.Next(4);
switch(c)
{
case 0:op = "+";result = a + b;break;
case 1:op = "-";result = a - b;break;
case 2:op = "*";result = a * b;break;
case 3:op = "/";result = a / b;break;
}
label1.Text = a.ToString();
label3.Text = b.ToString();
label2.Text = op;
textBox1.Text = "";
}
///
/// 判断
///
///
///
private void button2_Click(object sender, EventArgs e)
{
string str = textBox1.Text;
double d = double.Parse(str);
string disp = " " + a + op + b + "=" + str + " ";
if (d == result)
disp += "☆";
else
disp += "╳";
listBox1.Items.Add(disp);
}
public Form1()
{
InitializeComponent();
}
}
}
句的在循环条件满足的情况下,反复执行特定代码;
① 初始化部分(init_statement);
② 循环条件部分(test_exp);
③ 循环体部分(body_statement);
④ 迭代部分(alter_statement)称为“循环改变”;
⑤ “结束后处理”;
s=0;for(int i=1;i<=100;i++)s+=i;
Console.Write(s);
for循环、while循环、do-while循环
① for循环语句
语法格式
for(init_statement;test_exp;alter_statement)
{
body_statement;
}
应用举例
int result=0;
for(int i=0;i<=100;i++)
{
result+=1;
}
System.Console.Write("result="+result);
② while 循环语句
语法格式
[init_statement]
while(test_exp)
{
body_statement;
[alter_statement;]
}
应用举例
int result=0;
int i=1;
while(i<=100)
{
result+=i;
i++;
}
System.Console.Write("result"+result);
③ do/while循环语句
至少执行一次
语法格式:
[init_statement]
do
{
body_statement;
[alter_statement;]
} while (test_exp);
应用举例
int result=0;int i=1;
do{
result+=i;
i++;
}while(i<=100)
System.Console.Write("result"+result);
常用的跳转语句:
① break语句
结束循环(相当于VB中的Exit Do,Exit For)
② continue语句
进入下一次循环;
③ goto语句
跳转到某个语句标号;
④ try{}catch{}语句
异常的捕获;
有关Goto语句的争论
C# 中的goto语句:
可以从内层跳到外层;
标号的写法:标识符加个冒号(
示例:
角谷猜想
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 角谷猜想
{
internal class Program
{
public static void Main(string[] args)
{
for(int n=1;n<=100;n++)
{
int a = n;
while(a!=1)
{
Console.Write(" " + a);
if (a % 2 == 1) a = a * 3 + 1;
else
a /= 2;
}
Console.WriteLine(" " + a);
}
Console.ReadKey();
}
}
}
画许多圆(使用循环与绘图)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 画许多圆
{
public partial class Form1 : Form
{
Random random = new Random();
public Form1()
{
InitializeComponent();
}
Color getRandomColor()
{
return Color.FromArgb(
random.Next(256),
random.Next(256),
random.Next(256));
}
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
int x0 = this.Width / 2;
int y0 = this.Height / 2;
for(int r=0;r<this.Height/2;r++)
{
g.DrawEllipse(
new Pen(getRandomColor(), 1),
x0 - r, y0 - r, r * 2, r * 2);
}
g.Dispose();
}
}
}