一个totalscore函数,返回值是三门课的成绩
时间: 2023-09-24 15:01:12 浏览: 64
要编写一个totalscore函数,可以这样实现:
```python
def totalscore():
score1 = float(input("请输入第一门课的成绩:"))
score2 = float(input("请输入第二门课的成绩:"))
score3 = float(input("请输入第三门课的成绩:"))
return score1, score2, score3
s1, s2, s3 = totalscore()
print("第一门课的成绩为:", s1)
print("第二门课的成绩为:", s2)
print("第三门课的成绩为:", s3)
```
在函数中,我们使用`input`函数来获取用户输入的成绩,并将其转换为浮点数类型。然后,将三门课的成绩作为元组返回。接下来,在调用函数时,我们将返回的三个成绩分别赋值给变量`s1`,`s2`和`s3`,并打印出来。
注意:这里假设输入的成绩是数字类型,如果需要进行数据类型检查或者进行其他处理,你可以在函数中加入相应的逻辑。此外,这里假设输入的成绩是合法且按照顺序输入的,如果存在其他输入要求或者校验逻辑,也可以在函数中进行处理。
相关问题
用C++编写以下程序:有 5 个学生,每个学生的数据包括学号、姓名、三门课成绩,从键盘输入 5 个学生的数据, 要求计算并输出: (1)每个学生三门课的总成绩。 (2)三门课每门课程的平均成绩。 指导: (1)定义一个学生类 TStudent,包含 4 个私有数据成员(学号、姓名、3 门功课的成绩 和总成绩),3 个公有成员函数(初始化对象的函数 Init、显示学生信息的函数 show_st 和得到 学生 3 门功课成绩的函数 GetScore)。 (2)在 Init 函数中求每个学生三门课的总成绩。 (3)在 main 函数中求三门课每门课程的平均成绩
以下是用C++编写的程序:
```c++
#include <iostream>
#include <string>
using namespace std;
class TStudent {
private:
string id;
string name;
int score[3];
int totalScore;
public:
void Init(string id, string name, int score1, int score2, int score3) {
this->id = id;
this->name = name;
this->score[0] = score1;
this->score[1] = score2;
this->score[2] = score3;
this->totalScore = score1 + score2 + score3;
}
void show_st() {
cout << "学生ID:" << id << endl;
cout << "学生姓名:" << name << endl;
cout << "三门课成绩:" << score[0] << " " << score[1] << " " << score[2] << endl;
cout << "总成绩:" << totalScore << endl;
}
int* GetScore() {
return score;
}
};
int main() {
TStudent students[5];
string id, name;
int score1, score2, score3;
// 输入学生信息
for (int i = 0; i < 5; i++) {
cout << "请输入第" << i + 1 << "个学生的信息:" << endl;
cout << "学生ID:";
cin >> id;
cout << "学生姓名:";
cin >> name;
cout << "三门课成绩:";
cin >> score1 >> score2 >> score3;
students[i].Init(id, name, score1, score2, score3);
}
// 计算平均成绩
int totalScore[3] = {0};
for (int i = 0; i < 5; i++) {
int* score = students[i].GetScore();
totalScore[0] += score[0];
totalScore[1] += score[1];
totalScore[2] += score[2];
}
double avgScore[3] = {0};
for (int i = 0; i < 3; i++) {
avgScore[i] = totalScore[i] / 5.0;
}
// 输出结果
for (int i = 0; i < 5; i++) {
students[i].show_st();
}
cout << "三门课程的平均成绩:" << avgScore[0] << " " << avgScore[1] << " " << avgScore[2] << endl;
return 0;
}
```
程序中定义了一个学生类 `TStudent`,包含学生的学号、姓名、三门课成绩和总成绩。在 `Init` 函数中,计算每个学生的总成绩。在 `main` 函数中,输入学生信息,计算每门课程的平均成绩,然后输出结果。
用Java编写一个程序,从键盘输入一个学生语文、数学、英语三门课的成绩,计算出三门课总成绩和平均分,并打印输出
好的,以下是Java代码实现:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入学生的语文成绩:");
double chinese = input.nextDouble();
System.out.print("请输入学生的数学成绩:");
double math = input.nextDouble();
System.out.print("请输入学生的英语成绩:");
double english = input.nextDouble();
double totalScore = chinese + math + english;
double averageScore = totalScore / 3;
System.out.println("总成绩:" + totalScore);
System.out.println("平均分:" + averageScore);
}
}
```
首先通过 `Scanner` 类从键盘输入学生的语文、数学、英语三门课的成绩,然后分别计算三门课的总成绩和平均分,最后打印输出总成绩和平均分。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)