基础测试题解答记录。

1、编写程序计算1+2+3+....+100的和。

int m=0;

for (int i= 0; i<= 100; i++)

{

m += i;

}

MessageBox.Show(m.ToString ());

 

2、已知一个int数组, 编程从数组中获取最大数。

int[] str={3,7,9,2,6,8,5,29};

int s = str[0];

for (int i = 0; i < str.Length; i++)

{

if (str[i] > s)

{

s = str[i];

}

}

MessageBox.Show(s.ToString ());

 3、用户输入一个“2008-01-02”格式的日期,分析用户输入的日期然后按照“2008年1月2日”的格式重新输出。

 

方法一:

string s = "2008-01-02";

string[] str = s.Split('-');

string newtimes = string.Format("{0}年{1}月{2}日", str[0], str[1].ToString()[1], str[2].ToString()[1]);

MessageBox.Show(newtimes);

 

 方法二:

string s = "2008-01-02";

DateTime dt = Convert.ToDateTime(s);

MessageBox.Show(dt.ToLongDateString());

 

 4、编写一个类Person,为Person类定义年龄、姓名两个属性,并且定义一个SayHello方法,方法执行时输出“我是***我的年龄是***”;定义一个Chinese类从Person类继承。

public class Person

{

public int old{ set; get;}

public string Uname{ set;get;}

public void SayHello()

{

Console.WriteLine("我是{0},我的年龄是{1}", Uname, old);

}

}

public class Chinese : Person

{

public string company { get; set; }

public void cia()

{

Console.WriteLine("我的名字叫{0},我{1}岁了,我来自{2}。", Uname, old, company);

}

}

class Program

{

static void Main(string[] args)

{

Chinese chzn=new Chinese ();

Console.WriteLine("名称:");

chzn.Uname = Console.ReadLine();

Console.WriteLine("年龄:");

chzn.old = Convert .ToInt32(Console.ReadLine());

Console.WriteLine("国籍:");

chzn.company = Console.ReadLine();

chzn.cia();

Console.ReadLine();

}

}

5、不借助于Dreamweaver、VisualStudio等开发工具,使用记事本等文本编辑器编写下面的HTML页面:

<html>

<head>

<title>test</title>

<style type="text/css">

table

{

background
: #F7F7F7;

border
: 1px #C7D1DD solid;

border-collapse
: collapse;

font-size
: 12px;

width
: 500px;

}

table th

{

background
: #C5E3FD;

text-align
: left;

}

table tr

{

height
: 31px;

}

table tr td

{

border
: 1px #CECECE solid;

}

.tright

{

text-align
: right;

width
: 152px;

}

.tcenter

{

text-align
: center;

}

.w89

{

width
: 89px;

}

.tfloat

{

float
: left;

display
: block;

margin
: 0;

padding
: 0;

}

input[type="text"], input[type="password"]

{

height
: 22px;

margin
: 2px;

}

.lineheight26

{

line-height
: 26px;

}

</style>

</head>

<body>

<form id="myform">

<table>

<tr>

<th colspan="2">

必填信息

</th>

</tr>

<tr>

<td class="tright">

登陆名

</td>

<td>

<input type="text" id="uname" class="tfloat" /><p id="altuname" class="tfloat lineheight26">

(只能用英文,数字和下划线)</p>

</td>

</tr>

<tr>

<td class="tright">

密码

</td>

<td>

<input id="Password1" type="password" class="tfloat"/><p id="altpass" class="tfloat lineheight26">

(密码必须大于6位,区分大小写)</p>

</td>

</tr>

<tr>

<td class="tright">

确认密码

</td>

<td>

<input id="Password2" type="password" class="tfloat"/><p id="altpass2" class="tfloat lineheight26">

</p>

</td>

</tr>

<tr>

<td class="tright">

邮件地址

</td>

<td>

<input type="text" id="mail1" class="tfloat"/><p id="altmail" class="tfloat lineheight26">

</p>

</td>

</tr>

<tr>

<td class="tright">

确认邮件地址

</td>

<td>

<input type="text" id="mail2" class="tfloat" /><p id="altmail2" class="tfloat lineheight26">

</p>

</td>

</tr>

<tr>

<td colspan="2" class="tcenter">

<input id="Submits" type="button" value="提交" class="w89" />

<input id="Resets" type="reset" value="重填" class="w89" />

</td>

</tr>

</table>

</form>

</body>

</html>

6、使用JavaScript验证第5题的HTML表单。要求如下:“登录名”必须是字母数字或下划线, 不能以数字开头;密码为6-16位字母、数字或者下划线.;密码和确认密码一致;邮件地址是正确的邮件地址格式;邮件地址和确认邮件地址一致。

<html>

<head>

<title>test</title>

<style type="text/css">

table

{

background
: #F7F7F7;

border
: 1px #C7D1DD solid;

border-collapse
: collapse;

font-size
: 12px;

width
: 500px;

}

table th

{

background
: #C5E3FD;

text-align
: left;

}

table tr

{

height
: 31px;

}

table tr td

{

border
: 1px #CECECE solid;

}

.tright

{

text-align
: right;

width
: 152px;

}

.tcenter

{

text-align
: center;

}

.w89

{

width
: 89px;

}

.tfloat

{

float
: left;

display
: block;

margin
: 0;

padding
: 0;

}

input[type="text"], input[type="password"]

{

height
: 22px;

margin
: 2px;

}

.lineheight26

{

line-height
: 26px;

}

</style>

<script type="text/javascript">

function resetname() {

document.getElementById(
"altuname").innerHTML = "(只能用英文,数字和下划线)";

}

function resetpass() {

document.getElementById(
"altpass").innerHTML = "(密码必须大于6位,区分大小写)";

}

function validate() {

var uname = document.getElementById("uname").value;

var altuname = document.getElementById("altuname").value;

var pass1 = document.getElementById("Password1").value;

var pass2 = document.getElementById("Password2").value;

var mail1 = document.getElementById("mail1").value;

var mail2 = document.getElementById("mail2").value;

var reguname = /^[a-zA-Z_][a-zA-Z0-9_]{0,16}$/;

var regpass1 = /^[a-z0-9_-]{6,16}$/;

var regmail = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;

var valiname=reguname.test(uname);

var valipass = regpass1.test(pass1);

var valimail = regmail.test(mail1);



if (valiname) {

document.getElementById(
"altuname").innerHTML = "<font color='green'>通过!</font>";

}

else {

document.getElementById(
"altuname").innerHTML = "<font color='red'>用户名不符合要求!</font>";

return;

}



if (valipass) {

document.getElementById(
"altpass").innerHTML = "<font color='green'>通过!</font>";

}

else {

document.getElementById(
"altpass").innerHTML = "<font color='red'>密码不符合要求!</font>";

return;

}

if (pass1==pass2) {

document.getElementById(
"altpass2").innerHTML = "<font color='green'>通过!</font>";

}

else {

document.getElementById(
"altpass2").innerHTML = "<font color='red'>两次输入的密码不一致!</font>";

return;

}





if (valimail) {

document.getElementById(
"altmail").innerHTML = "<font color='green'>通过!</font>";

}

else {

document.getElementById(
"altmail").innerHTML = "<font color='red'>邮箱不符合要求!</font>";

return;

}

if (mail1==mail2) {

document.getElementById(
"altmail2").innerHTML = "<font color='green'>通过!</font>";

}

else {

document.getElementById(
"altmail2").innerHTML = "<font color='red'>两次输入的邮箱不一致!</font>";

return;

}

}

</script>

</head>

<body>

<form id="myform">

<table>

<tr>

<th colspan="2">

必填信息

</th>

</tr>

<tr>

<td class="tright">

登陆名

</td>

<td>

<input type="text" id="uname" class="tfloat" onfocus="resetname()" onblur="validate()" /><p id="altuname" class="tfloat lineheight26">

(只能用英文,数字和下划线)</p>

</td>

</tr>

<tr>

<td class="tright">

密码

</td>

<td>

<input id="Password1" type="password" class="tfloat" onfocus="resetpass()" onblur="validate()" /><p id="altpass" class="tfloat lineheight26">

(密码必须大于6位,区分大小写)</p>

</td>

</tr>

<tr>

<td class="tright">

确认密码

</td>

<td>

<input id="Password2" type="password" class="tfloat" onblur="validate()" /><p id="altpass2" class="tfloat lineheight26">

</p>

</td>

</tr>

<tr>

<td class="tright">

邮件地址

</td>

<td>

<input type="text" id="mail1" class="tfloat" onblur="validate()" /><p id="altmail" class="tfloat lineheight26">

</p>

</td>

</tr>

<tr>

<td class="tright">

确认邮件地址

</td>

<td>

<input type="text" id="mail2" class="tfloat" onblur="validate()" /><p id="altmail2" class="tfloat lineheight26">

</p>

</td>

</tr>

<tr>

<td colspan="2" class="tcenter">

<input id="Submits" type="button" value="提交" onclick="validate()" class="w89" />

<input id="Resets" type="reset" value="重填" class="w89" />

</td>

</tr>

</table>

</form>

</body>

</html>


 

posted @ 2012-03-07 15:08  ``炯``  阅读(519)  评论(0)    收藏  举报