switch语句写跑马灯
时间: 2023-11-29 08:43:29 浏览: 38
switch语句可以用来实现跑马灯效果。跑马灯是一种在页面上循环滚动显示内容的效果。下面是一个使用switch语句实现跑马灯的例子:
```
<script>
var text = "这是跑马灯的内容,可以根据需要修改。";
var i = 0;
function run() {
document.getElementById("marquee").innerHTML = text.substring(i, text.length) + text.substring(0, i);
i++;
if (i > text.length) {
i = 0;
}
setTimeout("run()", 100);
}
run();
</script>
<div id="marquee"></div>
```
在这个例子中,我们定义了一个变量text来保存跑马灯的内容,然后定义了一个变量i来记录当前显示的位置。在run函数中,我们使用substring方法来截取text字符串的一部分,并将其赋值给marquee元素的innerHTML属性,从而实现跑马灯效果。最后,我们使用setTimeout方法来定时调用run函数,从而实现循环滚动的效果。
相关问题
switch语句写分段函数
以下是使用switch语句写分段函数的示例代码:
```c
#include <stdio.h>
int main() {
float x;
int k;
scanf("%f", &x);
k = (x >= 0.5) + (x >= 1.5) + (x >= 2.5) + (x >= 3.5) + (x >= 4.5); //真为1 假为0
switch (k) {
case 1:
printf("%.2f\n", 3 + 2 * x);
break;
case 2:
printf("%.2f\n", 3 - 2 * x);
break;
case 3:
printf("%.2f\n", 3 * 2 * x);
break;
case 4:
printf("%.2f\n", 3 / (2 * x));
break;
default:
printf("数值越界");
}
return 0;
}
```
该程序根据用户输入的x值,将其分为五个区间,然后根据不同的区间进行不同的计算并输出结果。其中,k的值表示x所在的区间,根据x是否大于等于区间的上限来计算k的值。最后,使用switch语句根据k的值来执行相应的计算并输出结果。
用switch语句写成绩等级
以下是使用switch语句写成绩等级的示例代码:
```python
def get_grade(score):
mark = ""
quotient = score // 10
switch(quotient):
case 9:
case 10:
mark = "A或B"
break
case 8:
mark = "A或B"
break
case 7:
case 7.9:
mark = "C"
break
case 6:
case 6.9:
mark = "D"
break
default:
mark = "F"
return mark
score = 85
grade = get_grade(score)
print("成绩等级为:", grade) # 输出:成绩等级为: A或B
score = 72
grade = get_grade(score)
print("成绩等级为:", grade) # 输出:成绩等级为: C
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)