Correcting teacher:灭绝师太
Correction status:qualified
Teacher's comments:买个小服务器做测试还是不错的~
php
//输出颜色不一样的九宫格
//随机颜色
for($i=0;$i<3;$i++){
$td="";
for($a=0;$a<3;$a++){
$r=mt_rand(0,255);
$g=mt_rand(0,255);
$b=mt_rand(0,255);
$td.=<<<td
<td style='background-color:rgb($r,$g,$b);'>td>
td;
}
$tr.=<<<tr
$td
tr>
tr;
}
echo<<<table
<table border="1"cellspacing="0"width="150"height="150">
$tr
table>
table;
二.用php实现具有简单功能的计算器
代码块
lang="en">
charset="UTF-8">
http-equiv="X-UA-Compatible"content="IE=edge">
name="viewport"content="width=device-width, initial-scale=1.0">
简单计算器 .box{
border:1pxsolid;
border-radius:2px;
background-color:lightskyblue;
box-shadow:6px2px3px0px#555;
width:450px;
margin:20pxauto;
height:150px;
}
td{
text-align:center;
}
php
//判断用户是否点击了计算按钮
if(isset($_POST['submit'])){
//判断用户除数不能为0
if($_POST['option']=='/'&&$_POST['number2']==0||$_POST['option']=='%'&&$_POST['number2']==0){
//设置一个变量来储存输出的字符
$tips='
除数不能为0
';}
}
?>
class="box">
colspan="4">简单计算器
=$_POST["number1"]?>" >
=$_POST["number2"]?>" >
type="submit"name="submit"value="计算">
colspan="4"> php
//判断 $tips 是否为0 是则输出语句 否则执行计算功能
if(!$tips){
switch($_POST['option']){
case"+":
//int 临时转换为数值类型
$sum=(int)$_POST['number1']+(int)$_POST['number2'];
break;
case"-":
$sum=(int)$_POST["number1"]-(int)$_POST['number2'];
break;
case"*":
$sum=(int)$_POST["number1"]*(int)$_POST['number2'];
break;
case"/":
$sum=(int)$_POST["number1"]/(int)$_POST['number2'];
break;
case"%":
$sum=(int)$_POST["number1"]%(int)$_POST['number2'];
break;
}
//计算结果通知到用户
$tipss="计算结果: {$_POST['number1']} {$_POST['option']} {$_POST['number2']} = {$sum} ";
echo"
$tipss
";}else{
echo $tips;
}
?>
三.用javaScript实现具有简单功能的计算器
代码块
lang="en">
charset="UTF-8"/>
http-equiv="X-UA-Compatible"content="IE=edge"/>
name="viewport"content="width=device-width, initial-scale=1.0"/>
计算器 rel="stylesheet"href="jsq.css"/>
class="jsq">
cellspacing="0";>
colspan="4">计算器
colspan="4">
colspan="4"> >
重置
>
//拿到input框
let input=document.querySelector("input");
//拿到所有的按钮
let btnS=document.querySelectorAll("button");
//拿到等于号求和
letSum=document.querySelector(".sum");
//拿到重置按钮
letReset=document.querySelector(".reset");
//给重置按钮添加点击事件,重置input值
Reset.addEventListener("click",(ev)=>{
input.value="";
});
//遍历所有的按钮,把value值添加到input上
btnS.forEach(function(item){
item.addEventListener("click",function(ev){
input.value+=item.value;
});
});
//'=' 添加事件求和input里面的值
Sum.addEventListener("click",function(ev){
let val=input.value;
if(val.includes("*")){
let setX=val.split("*");
let take=1;
setX.forEach((item)=>{
let items=item*1;
take*=items;
});
input.value="";
input.value=take;
}elseif(val.includes("+")){
let setX=val.split("+");
let add=0;
setX.forEach((item)=>{
let items=item*1;
add+=items;
});
input.value="";
input.value=add;
}elseif(val.includes("/")){
//更新方式采用key值进行传输
let setX=val.split("/");
let setX1=setX[0];
let setX2=setX[1];
let devide=setX1/setX2;
input.value="";
input.value=devide;
}elseif(val.includes("-")){
//更新方式采用key值进行传输
let setX=val.split("-");
let setX1=setX[0];
let setX2=setX[1];
let minus=setX1-setX2;
input.value="";
input.value=minus;
}
});
单击->进入计算器模拟测试
http://www.zwzgjl.com/error/jsq/jsq.html
Statement of this WebsiteThe copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!All comments Speak rationally on civilized internet, please comply withNews Comment Service Agreement0 commentsAuthor's latest blog post
2021-04-23 00:09:02
- About us Disclaimer Sitemap
- php.cn:Public welfare online PHP training,Help PHP learners grow quickly!