从键盘输入若干同学的分数(百分制整数)
时间: 2023-11-23 20:05:56 浏览: 36
好的,以下是 Python 代码实现:
```python
scores = []
while True:
score = input("请输入同学的分数(输入 q 结束):")
if score == "q":
break
score = int(score)
if score < 0 or score > 100:
print("输入有误,请重新输入")
continue
scores.append(score)
print("输入的分数为:", scores)
```
这段代码通过一个 while 循环,不断从键盘输入同学的分数,直到输入 `q` 结束循环。每次输入的分数都会被转换成整数,然后判断是否在 0 到 100 之间,如果不是则会提示输入有误并重新输入。如果输入正确,则将分数保存在一个列表中。最后输出保存的所有分数。
相关问题
C语言输入整数输出百分制
可以使用 printf 函数进行格式化输出,具体代码如下:
```c
#include <stdio.h>
int main() {
int score;
printf("请输入分数:");
scanf("%d", &score);
printf("百分制成绩为:%d\n", score);
return ;
}
```
其中,`%d` 表示输出整数,`\n` 表示换行。
用c++从键盘输入一百个整数
好的,以下是使用C++语言从键盘输入一百个整数的代码:
```c++
#include <iostream>
using namespace std;
int main() {
int numbers[100];
for (int i = 0; i < 100; i++) {
cout << "请输入第" << i+1 << "个整数:";
cin >> numbers[i];
}
cout << "您输入的100个整数为:";
for (int i = 0; i < 100; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
```
请您在控制台或者集成开发环境中执行该代码,并按照提示输入100个整数,程序会将您输入的100个整数打印输出。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)