想要自己寫一個電腦應用程式,費了大概一天的時間,結果還是在高人的指點下才完成(在這裡謝謝一直輔導我的哥哥),原來代碼不是那麼好些的,自己以後要嘗試多寫一些,大膽,不要怕錯,細心調試,慢慢更改,我相信一定行的!加油!!!自己是最棒的!!!!
為什麼說是簡單數字電腦,應為這個電腦還不能夠辨識數字和漢字,隻能輸入純數字以進行加減乘除簡單運算,随着學習的更進一步深入,會更加完善!
html代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="電腦._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>簡單電腦</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="num1" runat="server" ></asp:TextBox>
<asp:DropDownList ID="dropdownlist" runat="server">
<asp:ListItem Value="加号" Selected="True">+</asp:ListItem>
<asp:ListItem Value="減号">-</asp:ListItem>
<asp:ListItem Value="乘">*</asp:ListItem>
<asp:ListItem Value="除">/</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="num2" runat="server"></asp:TextBox><br/>
<asp:Label ID="Label1" runat="server" BackColor="#339966" Text="結果是:"
Width="200px"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="計算" Width="88px"
onclick="Button1_Click" />
</div>
</form>
</body>
</html>
背景c#代碼:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace 電腦
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
int nu1 = int.Parse(this.num1.Text.ToString());//對nu1從string轉為int型。
int nu2 = int.Parse(this.num2.Text.ToString());
int sum = 0;//對sum進行初始化
string n = this.dropdownlist.SelectedItem.Text.ToString();//選擇下拉單選框中選擇在aspx源中的+,-,*,/。
switch (n)
{
case ("+"):
sum = nu1 + nu2;
break;
case "-":
sum = nu1 - nu2;
case "*":
sum = nu1 * nu2;
case "/":
sum = nu1 / nu2;
default:
Console.WriteLine("請正确輸入數字");
}
this.Label1.Text = sum.ToString();//在空間label中顯示sum的值。
}
}
本文轉自shenzhoulong 51CTO部落格,原文連結:http://blog.51cto.com/shenzhoulong/294314,如需轉載請自行聯系原作者