》1《 找出数组中的最大值和最小值
先设计一下页面像这个样子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int maxNum, minNum;
int []number = new int[8]{21, 23, 54, 63, 7, 6, 87, 333};
maxNum = minNum = number[0];
/*
foreach(object in set){
* // TODO
* }
*/
foreach (int x in number) {
if (x > maxNum) {
maxNum = x;
}
if (x < minNum) {
minNum = x;
}
}
TextBox1.Text = "最大元素是" + maxNum.ToString() + ",最小元素是" + minNum.ToString();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
}
》2《 数组元素排序
直接在网页代码中嵌入C#代码:
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
int[] array = new int[] {21,4,5,3,1,2,34,54,67,123};
/*
Array.Sort(ArrayName, StartSort, EndSort);
*/
Array.Sort(array, 0, array.Length);
foreach (int i in array) {
TextBox1.Text = TextBox1.Text + i.ToString() + " ";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 21px" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server" Width="480px"></asp:TextBox>
</div>
</form>
</body>
</html>
运行效果:
》3《 新建网站文件,在设计视图中输入三行文字,分别是用户登录、用户名、密码、在用户名和密码后面分别添加一个文本框TextBox1、TextBox2,继续添加两个按钮,并将其text属性改为“确定”、“取消”。
随即得到代码:
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "admin" )
{
Response.Write("Administrator logined");
}else if(TextBox1.Text == "firedom"){
Response.Write("Hello, firedom~!");
}else{
Response.Write("Require all denied");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
.auto-style1 {
text-align: center;
}
#Password1 {
width: 130px;
}
</style>
</head>
<body style="text-align: center">
<form id="form1" runat="server">
<div class="auto-style1">
<h1 class="auto-style1">
<br />
<br />
FIREDOM网站登陆页面</h1>
<div class="auto-style1">
<br />
用户名:<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" Width="130px"></asp:TextBox>
<br />
密码:<input id="Password1" type="password" /><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="确定" />
<asp:Button ID="Button2" runat="server" Text="取消" OnClick="Button2_Click" />
</div>
</div>
</form>
</body>
</html>
执行效果:
》4《 随机点名系统
设计以下代码:
<%@ Page Language="C#" %>
<%@ Import Namespace ="System.Threading" %>
<!DOCTYPE html>
<script runat="server">
String[] className = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
"31", "32" };
Random rnd = new Random();
protected void Button1_Click(object sender, EventArgs e)
{
int n = rnd.Next(0, 31);
Label2.Text = className[n];
// System.Threading.Thread.Sleep(50);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
.auto-style1 {
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1 class="auto-style1">
<asp:Label ID="Label1" runat="server" style="font-size: 80pt" Text="计科12点名系统"></asp:Label>
</h1>
<p class="auto-style1">
<asp:Label ID="Label2" runat="server" style="font-size: 80pt; font-weight: 700;" Text="press start"></asp:Label>
</p>
<p class="auto-style1">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="开始抽奖" />
</p>
</div>
</form>
</body>
</html>
执行效果:
OVER