《C++ Primer Plus》学习笔记——第二章 初识C++(三)

本篇进行编程练习

1.编写一个C++程序,它显示您的姓名和喜好。

#include <iostream>

int main ()
{
	using namespace std;

	cout<<"My name is GM_AMRC."<<endl;
	cout<<"I love play computer game."<<endl;
	return 0;
}

显示结果为:

My name is GM_AMRC.
I love play computer game.

2.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月。

#include <iostream>

int main ()
{
	using namespace std;

	int age;
	cout<<"Enter your age:";
	cin>>age;
	cout<<"It has "<<age*12<<" months."<<endl;

	return 0;
}

显示结果为:

Enter your age:29
It has 348 months.

3.编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,以9:10这样的格式显示这两个值。

#include <iostream>

using namespace std;

void Output(int hours,int mins);

int main ()
{
	int hours;
	cout<<"Enter the number of hours:";
	cin>>hours;
	int mins;
	cout<<"Enter the number of minutes:";
	cin>>mins;
	Output(hours,mins);

	return 0;
}

void Output(int hours,int mins)
{
	cout<<"Time: "<<hours<<":"<<mins<<endl;
}

显示结果为:

Enter the number of hours:9
Enter the number of minutes:10
Time: 9:10

猜你喜欢

转载自blog.csdn.net/GM_AMRC/article/details/83021317