使用嵌套 If Else 为学生分配成绩的程序
给定一个整数数组mark ,其中包含学生在不同科目中的得分(满分 100 分),任务是为学生分配一个成绩。成绩是通过计算学生得分的百分比来确定的。百分比计算如下:
使用以下规则分配等级:Percentage Grade 90 and above A 80 to 89 B 60 to 79 C 33 – 59 D below 33 F
例子:
Input: marks = { 25, 65, 46, 98, 78, 65 }
Output: C
Input: marks = { 95, 88, 98, 93, 92, 96 }
Output: A
方法:
- 初始化一个变量,将学生得分的所有分数相加,总计为 0。
- 初始化一个变量来存储学生的成绩,成绩为'F'。
- 首先,我们遍历标记数组并找到学生的总分。
- 然后,我们应用上述公式来计算百分比。
- 然后我们创建一个嵌套的 if else 构造来为学生分配适当的成绩。
有关决策制定和不同类型的决策制定结构的更多信息,请参阅Java中的决策制定。
下面是上述方法的实现:
C++
// CPP program to assign grades to a student
// using nested if-else
#include
using namespace std;
int main()
{
// Store marks of all the subjects in an array
int marks[] = { 25, 65, 46, 98, 78, 65 };
// Max marks will be 100 * number of subjects
int len = sizeof(marks) / sizeof(marks[0]);
int max_marks = len * 100;
// Initialize student's total marks to 0
int total = 0;
// Initialize student's grade marks to F
char grade = 'F';
// Traverse though the marks array to find the sum.
for (int i = 0; i < len; i++)
{
total += marks[i];
}
// Calculate the percentage.
// Since all the marks are integer,
// typecast the calculation to double.
double percentage = ((double)(total) / max_marks) * 100;
// Nested if else
if (percentage >= 90)
{
grade = 'A';
}
else
{
if (percentage >= 80 && percentage <= 89)
{
grade = 'B';
}
else
{
if (percentage >= 60 && percentage <= 79)
{
grade = 'C';
}
else
{
if (percentage >= 33 && percentage <= 59)
{
grade = 'D';
}
else
{
grade = 'F';
}
}
}
}
cout << (grade) << endl;;
}
// This code is contributed by
// Surendra_Gangwar
Java
// Java program to assign grades to a student
// using nested if-else
class GFG {
public static void main(String args[])
{
// Store marks of all the subjects in an array
int marks[] = { 25, 65, 46, 98, 78, 65 };
// Max marks will be 100 * number of subjects
int max_marks = marks.length * 100;
// Initialize student's total marks to 0
int total = 0;
// Initialize student's grade marks to F
char grade = 'F';
// Traverse though the marks array to find the sum.
for (int i = 0; i < marks.length; i++) {
total += marks[i];
}
// Calculate the percentage.
// Since all the marks are integer,
// typecast the calculation to double.
double percentage
= ((double)(total) / max_marks) * 100;
// Nested if else
if (percentage >= 90) {
grade = 'A';
}
else {
if (percentage >= 80 && percentage <= 89) {
grade = 'B';
}
else {
if (percentage >= 60 && percentage <= 79) {
grade = 'C';
}
else {
if (percentage >= 33 && percentage <= 59) {
grade = 'D';
}
else {
grade = 'F';
}
}
}
}
System.out.println(grade);
}
}
Python3
# Python3 program to assign grades
# to a student using nested if-else
if __name__ == "__main__":
# Store marks of all the subjects
# in an array
marks = [25, 65, 46, 98, 78, 65 ]
# Max marks will be 100 * number
# of subjects
max_marks = len(marks)* 100
# Initialize student's total
# marks to 0
total = 0
# Initialize student's grade
# marks to F
grade = 'F'
# Traverse though the marks array
# to find the sum.
for i in range(len(marks)):
total += marks[i]
# Calculate the percentage.
# Since all the marks are integer,
percentage = ((total) /max_marks) * 100
# Nested if else
if (percentage >= 90):
grade = 'A'
else :
if (percentage >= 80 and
percentage <= 89) :
grade = 'B'
else :
if (percentage >= 60 and
percentage <= 79) :
grade = 'C'
else :
if (percentage >= 33 and
percentage <= 59) :
grade = 'D'
else:
grade = 'F'
print(grade)
# This code is contributed by ita_c
C#
// C# program to assign grades to a student
// using nested if-else
using System;
class GFG
{
public static void Main()
{
// Store marks of all the subjects
// in an array
int []marks = { 25, 65, 46, 98, 78, 65 };
// Max marks will be 100 * number
// of subjects
int max_marks = marks.Length * 100;
// Initialize student's total marks to 0
int total = 0;
// Initialize student's grade marks to F
char grade = 'F';
// Traverse though the marks array to
// find the sum.
for (int i = 0; i < marks.Length; i++)
{
total += marks[i];
}
// Calculate the percentage.
// Since all the marks are integer,
// typecast the calculation to double.
double percentage = ((double)(total) /
max_marks) * 100;
// Nested if else
if (percentage >= 90)
{
grade = 'A';
}
else
{
if (percentage >= 80 && percentage <= 89)
{
grade = 'B';
}
else
{
if (percentage >= 60 && percentage <= 79)
{
grade = 'C';
}
else
{
if (percentage >= 33 && percentage <= 59)
{
grade = 'D';
}
else
{
grade = 'F';
}
}
}
}
Console.WriteLine(grade);
}
}
// This code is contributed by Ryuga
PHP
= 90)
{
$grade = 'A';
}
else
{
if ($percentage >= 80 && $percentage <= 89)
{
$grade = 'B';
}
else
{
if ($percentage >= 60 && $percentage <= 79)
{
$grade = 'C';
}
else
{
if ($percentage >= 33 && $percentage <= 59)
{
$grade = 'D';
}
else
{
$grade = 'F';
}
}
}
}
echo $grade . "\n";
// This code is contributed by Akanksha Rai
?>
Javascript
输出:
C