实 验 目 的 | 掌握索引和关联数组,以及下标和元素概念; 掌握数组创建、初始化,以及元素添加、删除、修改操作; 掌握foreach作用、语法、执行过程和使用; 能应用数组输出表格和数据。 |
任务1:使用一维索引数组存储医生年龄(随机生成一组年龄数组,年龄范围为22-60),使用foreach找出最大年龄、最小龄,算出平均年龄。
任务2:使用二维关联数组描述下表学生信息,并用表格输出学生信息,要求算出单科平均成绩。扩展(选做):借助数组函数分别按单科成绩从高到低排序。
姓名 | 英语成绩 | 数学成绩 | 语文成绩 |
张三 | 78 | 99 | 87 |
李四 | 88 | 88 | 79 |
老五 | 65 | 90 | 93 |
平均成绩 | 77 | ? | ? |
任务一:先搭建首页的页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.box {
width: 800px;
height: 200px;
background-color: #e4dfdf;
margin: 100px auto;
padding-top: 15px;
}
.box h1 {
text-align: center;
font-weight: 400;
}
.box .num {
width: 400px;
height: 50px;
}
form {
margin-top: 55px;
font-size: 22px;
}
</style>
</head>
<body>
<div class="box">
<h1>求医生年龄的最值</h1>
<form action="doctor_ages.php">
请输入您要生成的多少位医生的年龄:<input type="number" name="number" class="num"></form></br>
</form>
</div>
</body>
</html>
首页页面是给用户去生成多少位医生的年龄。
生成了多少位医生的年龄后去求医生的最大年龄和最小年龄,和平均年龄。
实验步骤:
<?php
$Dnumber = $_REQUEST["number"];
Calculate($Dnumber);
function Calculate($n){
//创建一个年龄数组
$ages =array();
for($i=0;$i<$n;$i++){
$ages[$i] =rand(20,60);
}
echo "生成的数组是:";
for($i=0;$i<$n;$i++){
if($i==($n-1)){
echo $ages[$i]." ";
}
else {
echo $ages[$i] . " , ";
}
}
echo "</br>";
$AgeMax = $ages[0];
$AgeMin = $ages[0];
$AgeSum=0;
for($i=0;$i<$n;$i++){
if($ages[$i]>$AgeMax){
$AgeMax=$ages[$i];
}
if($ages[$i]<$AgeMin){
$AgeMin=$ages[$i];
}
$AgeSum=$AgeSum+$ages[$i];
}
echo "医生的最大年龄是".$AgeMax."</br>";
echo "医生的最小年龄是".$AgeMin."</br>";
echo "医生的平均年龄".round($AgeSum/count($ages),2);
}
?>
任务二:
使用HTML+CSS搭建琦基础页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<link rel="stylesheet" href="https://cdn.staticfile.net/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.net/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.net/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
h1 {
text-align: center;
}
table {
margin: 0 auto;
}
tr h2 {
margin-left: 20px;
}
form input {
width: 70%;
height: 40px;
margin-bottom: 5px;
border-radius: 10px;
}
form label {
padding-left: 20px;
}
form button {
background-color: #5bc0de;
border-color: #46b8da;
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 18px;
font-weight: 400;
line-height: 1.42857143;
text-align: center;
white-space: nowrap;
vertical-align: middle;
-ms-touch-action: manipulation;
touch-action: manipulation;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-image: none;
border-radius: 4px;
margin-left: 90px;
margin-top: 10px;
width: 69%;
}
form button a {
color: #fff;
}
form button a:hover {
color: #18f508;
text-decoration: none;
}
</style>
</head>
<body>
<table width="80%" border="0">
<tr>
<td colspan="2" style="background-color:#FFA500;">
<h1>学生成绩查询</h1>
</td>
</tr>
<tr>
<td style="background-color:#FFD700;width:200px;vertical-align:top;">
<h2>添加学生成绩信息</h2>
<form method="get" action="">
<label for="name">学生 姓名:</label>
<input type="text" id="name" name="name" placeholder="姓名">
<br>
<label for="englishScore">英语 成绩:</label>
<input type="number" id="englishScore" name="englishScore" min="0" max="100" placeholder="英语成绩">
<br>
<label for="mathScore">数学 成绩:</label>
<input type="number" id="mathScore" name="mathScore" min="0" max="100" placeholder="数学成绩">
<br>
<label for="chineseScore">语文 成绩:</label>
<input type="number" id="chineseScore" name="chineseScore" min="0" max="100" placeholder="语文成绩">
<br>
<button type="submit" name="submit">
<a href="">添 加</a>
</button>
<h2>删除学生成绩信息</h2>
<label for="name">学生 姓名:</label>
<input type="text" id="name" name="dname" placeholder="姓名">
<br>
<button type="submit" name="dsubmit">
<a href="">删 除</a>
</button>
</form>
</td>
完整的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<link rel="stylesheet" href="https://cdn.staticfile.net/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.net/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.net/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
h1 {
text-align: center;
}
table {
margin: 0 auto;
}
tr h2 {
margin-left: 20px;
}
form input {
width: 70%;
height: 40px;
margin-bottom: 5px;
border-radius: 10px;
}
form label {
padding-left: 20px;
}
form button {
background-color: #5bc0de;
border-color: #46b8da;
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 18px;
font-weight: 400;
line-height: 1.42857143;
text-align: center;
white-space: nowrap;
vertical-align: middle;
-ms-touch-action: manipulation;
touch-action: manipulation;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-image: none;
border-radius: 4px;
margin-left: 90px;
margin-top: 10px;
width: 69%;
}
form button a {
color: #fff;
}
form button a:hover {
color: #18f508;
text-decoration: none;
}
</style>
</head>
<body>
<table width="80%" border="0">
<tr>
<td colspan="2" style="background-color:#FFA500;">
<h1>学生成绩查询</h1>
</td>
</tr>
<tr>
<td style="background-color:#FFD700;width:200px;vertical-align:top;">
<h2>添加学生成绩信息</h2>
<form method="get" action="">
<label for="name">学生 姓名:</label>
<input type="text" id="name" name="name" placeholder="姓名">
<br>
<label for="englishScore">英语 成绩:</label>
<input type="number" id="englishScore" name="englishScore" min="0" max="100" placeholder="英语成绩">
<br>
<label for="mathScore">数学 成绩:</label>
<input type="number" id="mathScore" name="mathScore" min="0" max="100" placeholder="数学成绩">
<br>
<label for="chineseScore">语文 成绩:</label>
<input type="number" id="chineseScore" name="chineseScore" min="0" max="100" placeholder="语文成绩">
<br>
<button type="submit" name="submit">
<a href="">添 加</a>
</button>
<h2>删除学生成绩信息</h2>
<label for="name">学生 姓名:</label>
<input type="text" id="name" name="dname" placeholder="姓名">
<br>
<button type="submit" name="dsubmit">
<a href="">删 除</a>
</button>
</form>
</td>
<td style="background-color:#eeeeee;height:550px;width:300px;vertical-align:top;padding-left:15px">
<?php
//二维数组
$student = array(
"张三" => array("英语成绩" => 78, "数学成绩" => 99, "语文成绩" => 87),
"李四" => array("英语成绩" => 88, "数学成绩" => 88, "语文成绩" => 79),
"王五" => array("英语成绩" => 65, "数学成绩" => 90, "语文成绩" => 93)
);
//平均值和最大值初始
$average = ["英语成绩" => 0, "数学成绩" => 0, "语文成绩" => 0];
$maxvalue = ["英语成绩" => 0, "数学成绩" => 0, "语文成绩" => 0];
//(添加修改)用的button标签判断是否null
if (isset($_REQUEST['submit'])) {
//获取数据
$name = $_REQUEST['name'];
$englishScore = $_REQUEST['englishScore'];
$chineseScore = $_REQUEST['chineseScore'];
$mathScore = $_REQUEST['mathScore'];
//判断数组中是否存在该键
if (array_key_exists($name, $student)) {
//存在提示
echo "修改成功";
} else {
//不存在提示
echo "添加成功!";
}
//存在替换,不存在添加
$student[$name] = array(
"语文成绩" => $chineseScore,
"数学成绩" => $mathScore,
"英语成绩" => $englishScore
);
}
//删除
if (isset($_REQUEST['dsubmit'])) {
//获取数据
$name = $_REQUEST['dname'];
//判断是否有键
if (array_key_exists($name, $student)) {
unset($student[$name]);
echo "删除成功";
} else {
echo "没有该学生!";
}
}
//遍历数组
if ($student) {
echo "<h2>信息</h2>";
echo "<table border='1'>";
echo "<tr>
<th width='150px'>姓名</th>
<th width='150px'>英语 成绩</th>
<th width='150px'>数学 成绩</th>
<th width='150px'>语文 成绩</th>
</tr>";
foreach ($student as $name => $subjects) {
//平均值先求和
$average["英语成绩"] += $subjects["英语成绩"];
$average["数学成绩"] += $subjects["数学成绩"];
$average["语文成绩"] += $subjects["语文成绩"];
echo "<tr>";
echo "<td>" . $name . "</td>";
//找出单科最高成绩
foreach ($subjects as $subject => $score) {
if ($score > $maxvalue[$subject]) {
$maxvalue[$subject] = $score;
}
echo "<td>" . $score . "</td>";
}
echo "</tr>";
}
//平均成绩:求和结果除个数
$average["英语成绩"] /= count($student);
$average["数学成绩"] /= count($student);
$average["语文成绩"] /= count($student);
//取整数
$average["英语成绩"] = round($average["英语成绩"], 0);
$average["数学成绩"] = round($average["数学成绩"], 0);
$average["语文成绩"] = round($average["语文成绩"], 0);
echo "<tr>";
echo "<td>" . "平均" . "\n" . "</td>" . "<td>" . $average["英语成绩"] . "\n" . "</td>" . "<td>" . $average["数学成绩"] . "\n" . "</td>" . "<td>" . $average["语文成绩"];
echo "</tr>";
echo "<tr>";
echo "<td>" . "单科最高成绩" . "</td>" . "<td>" . $maxvalue["英语成绩"] . "</td>" . "<td>" . $maxvalue["数学成绩"] . "</td>" . "<td>" . $maxvalue["语文成绩"] . "</td>";
echo "</tr>";
echo "</table>";
}
//运用数组函数排序
rsort($student["张三"]);
rsort($student["李四"]);
rsort($student["王五"]);
echo "<br>";
echo "张三成绩降序排序:";
foreach ($student["张三"] as $va) {
echo "\n" . $va;
}
// echo "张三成绩排序:".$student["张三"][0]."\n".$student["张三"][1]."\n".$student["张三"][2];
echo "<br>";
echo "李四成绩降序排序:";
foreach ($student["李四"] as $va) {
echo "\n" . $va;
}
echo "<br>";
echo "王五成绩降序排序:";
foreach ($student["王五"] as $va) {
echo "\n" . $va;
}
?>
</td>
</tr>
</table>
</body>
</html>
注意:本实验是笔者在校老师布置的任务。