//程序 1
//This program correctly averages 3 test scores.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int scorel, score2, score3;
double average;
//Get the three test scores
cout << "Enter 3 test scores and I will average them: ";
cin >> scorel >> score2 >> score3;
// Calculate and display the average score
average = (scorel + score2 + score3) / 3.0;
cout << fixed << showpoint << setprecision(1);
cout << "Your average is " << average << endl;
// If the average equals 100, congratulate the user
if (average == 100)
{
cout << "Congratulations ! ";
cout << "That's a perfect score!\n";
}
return 0;
}
程序第1次输出结果:
Enter 3 test scores and I will average them: 80 90 70
Your average is 80.0
程序第2次输出结果:
Enter 3 test scores and I will average them: 100 100 100
Your average is 100.0
Congratulations! That's a perfect score!
if (average == 100)
{
cout << "Congratulations!";
cout << "That's a perfect score!\n";
}
需要注意的是,有条件执行的语句块被大括号包围。当两个或多个动作与 if 语句关联时,这是必需的。如果只有一个语句被有条件地执行,则可以省略大括号。
if (average == 100)
cout << "Congratulations ! That's a perfect score ! \n";
当然,有些教师更喜欢让学生总是使用大括号包围有条件执行的块,即使它只包含一个语句也是如此。
// This program uses the modulus operator to determine
// if a number is odd or even. If the number is evenly divisible
// by 2, it is an even number. A remainder indicates it is odd.
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter an integer and I will tell you if it is odd or even: ";
cin >> number;
if (number % 2 == 0)
cout << number << " is even.\n";
else
cout << number << " is odd.\n";
return 0;
}
程序输出结果
Enter an integer and I will tell you if it is odd or even: 17
17 is odd.
number%2 不等于 0 时,会显示一条消息,指示号码为奇数。请注意,该程序将只釆用 if-else 语句中的两条路径之一。如果将计算机程序中的语句视为走在路上的脚步,那么 if-else 语句则可以视为道路中的分叉。它导致程序的执行只能按照两条分叉道路中的一条来走。
if天气很冷,那就穿一件棉衣;
else, if 天气只是有点凉,那就穿一件夹克;
else, if 有点起风,那就穿一件风衣;
else, if 天气很热,那就不要穿外套。
#include <iostream>
using namespace std;
int main()
{
// Create named constants to hold minimum
// scores required for each letter grade.
const int MIN_A_SCORE = 90,
MIN_B_SCORE = 80,
MIN_C_SCORE = 70,
MIN_D_SCORE = 60,
MIN_POSSIBLE_SCORE = 0;
int testScore; // Holds a numeric test score
char grade; // Holds a letter grade
bool goodScore = true;
// Get the numeric score
cout << "Enter your numeric test score and I will\n";
cout << "tell you the letter grade you earned: ";
cin >> testScore;
// Determine the letter grade
if (testScore >= MIN_A_SCORE)
grade = 'A';
else if (testScore >= MIN_B_SCORE)
grade = 'B';
else if (testScore >= MIN_C_SCORE)
grade = 'C';
else if (testScore >= MIN_D_SCORE)
grade = 'D';
else if (testScore >= MIN_POSSIBLE_SCORE)
grade = 'F';
else
goodScore = false; // The score was below 0
//Display the letter grade
if (goodScore)
cout << "Your grade is " << grade << ".\n";
else
cout << "The score cannot be below zero. \n";
return 0;
}
程序输出结果:
Enter your numeric test score and I will
tell you the letter grade you earned: 88
Your grade is B.
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有