1、练习一
1)a除以b取整数
- a = 10,b=3
<script>
let a = 10,b = 3;
let result = parseInt(a / b);
console.log(`运算结果为:${result}`);
</script>
2)a取b的模,将获取的结果转换为字符串类型
- a = 10,b=3
<script>
let a = 10,b = 3;
let result = String(parseInt(a % b));
console.log(`运算结果为:${result}`);
console.log(`结果类型为:${typeof result}`);
</script>
3)输入一个数字作为秒数,按照 “时:分:秒” 的格式输出
<script>
let num = parseInt(prompt("请输入要换算的秒数"));
let hours = parseInt(num / 3600);
let min = parseInt(num % 3600 / 60);
let second = parseInt(num % 60);
alert(`${num}秒等于:${hours}小时:${min}分钟:${second}秒`);
</script>
4) 如果小明语文数学成绩都是100分,周末去游乐园,否则在家改错题;
<script>
let chinese = Number(prompt("请输入语文成绩(0 ~ 100)"));
let math = Number(prompt("请输入数学成绩(0 ~ 100)"));
chinese == 100 && math == 100?alert(`成绩合格,周末去游乐园`):alert(`成绩不合格,在家改错题`);
</script>
5)如果小明语文数学成绩都是100分,并且奥数获得华杯赛前90名,去迪斯尼乐园,否则后年在努力
<script>
let chinese = Number(prompt("请输入语文成绩(0 ~ 100)"));
let math = Number(prompt("请输入数学成绩(0 ~ 100)"));
let morank = Number(prompt("请输入奥数名次"));
chinese == 100 && math == 100 && morank <= 90?alert(`成绩合格,去迪斯尼乐园`):alert(`成绩不合格,后年在努力`);
</script>
6)成绩等级查询
- A = 90 ~ 100
- B = 80 ~ 90
- C = 70 ~ 80
- D = 60 ~ 70
- E = 0 ~ 60
<script>
let Num = Number(prompt("请输入成绩"));
if (Num <= 100 && Num >= 90){
alert(`成绩等级为:A`);
}
else if (Num < 90 && Num >= 80){
alert(`成绩等级为:B`);
}
else if (Num < 80 && Num >= 70){
alert(`成绩等级为:C`);
}
else if (Num < 70 && Num >= 60){
alert(`成绩等级为:D`);
}
else if (Num <60 && Num >= 0){
alert(`成绩等级为:E`);
}
else{
alert(`成绩输入有误,请重新输入`);
}
</script>
7)输入3个数,找出最大数
<script>
//注意:这里演示代码,输入数字时,需要输入三个不相同的数字
let numOne = Number(prompt("请输入第一个数"));
let numTwo = Number(prompt("请输入第二个数"));
let numThr = Number(prompt("请输入第三个数"));
if (numOne < numTwo){
if (numTwo < numThr){
alert(`最大数为第三个数:${numThr}`);
}
else{
alert(`最大数为第二个数:${numTwo}`);
}
}
else{
if (numOne < numThr){
alert(`最大数为第三个数:${numThr}`);
}
else{
alert(`最大数为第一个数:${numOne}`);
}
}
</script>
练习二
1)如果输入的是1~6 的整数,则程序输出:your number is in 1~6,用户输入的数,如果输入的是其它数字,输出:Your number is too large,用户输入的数
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title>demo</title>
</head>
<body>
<script>
let num = Number(prompt("请输入一个数字"));
if (num <= 6 && num >= 1) {
alert(`your number in 1 ~ 6,your number is ${num}`);
}
else {
alert(`Your number is too large,your number is ${num}`);
}
</script>
</body>
</html>
2)输入numOne、numTwo、numThr 三个个位数(0 ~ 9)之间,组成一个最大的三位数
<script>
let numOne = Number(prompt("请输入第一个数字(个位数字,三个数不能相同)"));
let numTwo = Number(prompt("请输入第二个数字(个位数字,三个数不能相同)"));
let numThr = Number(prompt("请输入第三个数字(个位数字,三个数不能相同)"));
let maxNum, midNum, minNum;
if (numOne == numTwo || numOne == numThr || numTwo == numThr) {
alert(`请输入三个不同的数字,您输入的数字为:${numOne}、${numTwo}、${numThr}`);
}
else if (numOne < 0 || numOne >= 10 || numTwo < 0 || numTwo >= 10 || numThr < 0 || numThr >= 10) {
alert(`请输入三个个位数字,您输入的数字为:${numOne}、${numTwo}、${numThr}`)
}
else {
if (numOne > numTwo && numOne > numThr) { //如果第一个数最大
maxNum = numOne; //设置最大数为第一个数,继续判断第二个数和第三个数大小
if (numTwo > numThr) { //如果第二个数比第三个数大
midNum = numTwo; //设置中间数为第二个数F
minNum = numThr; //设置最小数为第三个数
}
else { //否则(第二个数比第三个数小)
midNum = numThr; //设置中间数为第三个数
minNum = numTwo; //设置最小数为第二个数
}
}
else if (numTwo > numOne && numTwo > numThr) { //如果第二个数最大
maxNum = numTwo; //设置最大数为第二个数,继续判断第一个数和第三个数大小
if (numOne > numThr) { //如果第一个数比第三个数大
midNum = numOne; //设置中间数为第一个数
minNum = numThr; //设置最小数为第三个数
}
else { //否则(第一个数比第三个数小)
midNum = numThr; //设置中间数为第三个数
minNum = numOne; //设置最小数为第一个数
}
}
else if (numThr > numOne && numThr > numTwo) { //如果第三个数最大
maxNum = numThr; //设置最大数为第三个数,继续判断第一个数和第二个数大小
if (numOne > numTwo) { //如果第一个数比第二个数大
midNum = numOne; //设置中间数为第一个数
minNum = numTwo; //设置最小数为第二个数
}
else { //否则(第一个数比第二个数小)
midNum = numTwo; //设置中间数为第二个数
minNum = numOne; //设置最小数为第一个数
}
}
alert(`输入的数字为:${numOne}、${numTwo}、${numThr},组成的最大数为:${maxNum}${midNum}${minNum}`)
}
</script>
3) 输入一个2000年 ~ 2500年之间的年份,并判断是否是闰年
- 能被4整除,但同时不能被100整除的年份是闰年
- 能被400整除的年份是闰年
- 其它情况都不是闰年
<script>
let years = Number(prompt("请输入年份(2000 ~ 2500)"));
if (years < 2000 && years > 2500) {
alert(`输入年份超出范围,请重新输入`);
}
else {
if (years % 4 == 0 && years % 100 !== 0 || years % 400 == 0) {
alert(`您输入的年份为:${years},${years}年是闰年`);
}
else {
alert(`您输入的年份为:${years},${years}年是平年`);
}
}
</script>
4)运输公司对用户计算运费.路程越远每公里运费越低
- 每公里每吨货物的基本运费为10,货物重为w,距离为s,折扣为d,
- 总运费f的计算公式为:f = 10 * w * s * (1 - d)
- s < 250km(没有折扣)
- 250<= s <500(2%折扣)
- 500<= s <1000(5%折扣)
- 1000<= s <2000(8%折扣)
- 2000<= s <3000(10%折扣)
- s >= 3000(15%折扣)
<script>
let w = Number(prompt("请输入货物重量"));
let s = Number(prompt("请输入运输距离"));
let d, f;
if (s <= 0 || w <= 0) {
alert(`输入有误,请重新输入,您输入的货物重量为:${w};您输入的运输距离为:${s};`)
}
else {
if (s < 250) {
d = 1;
}
else if (s >= 250 && s < 500) {
d = 1 - 0.02;
}
else if (s >= 500 && s < 1000) {
d = 1 - 0.05;
}
else if (s >= 1000 && s < 2000) {
d = 1 - 0.08;
}
else if (s >= 2000 && s < 3000) {
d = 1 - 0.1;
}
else {
d = 1 - 0.15;
}
f = 10 * w * s * (d);
alert(`本次货物运输费用为:${f}`);
}
</script>