|
【实训目的】综合运用本章技能
【第 1 步】实训内容介绍
实训内容介绍
编写一个程序,判断一个从键盘输入的数的正负和奇偶
实训结束
【Jitor 校验第 1 步】我已阅读实训内容介绍 // 送分题。直接点击。
【第 2 步】编写一个程序,判断一个从键盘输入的数的正负和奇偶
1、本实训使用项目 cpp3 中的 cpp3code.cpp 源代码文件,如果不存在,则先创建项目,然后创建源代码文件。
2、将下述代码复制到 cpp3code 中:
#include <iostream.h>
void main(void) {
}
然后编写一个程序,按照下述要求编写程序:
设计 C++ 程序,判断从键盘输入的整数的正负性和奇偶性:
为 0 时,输出:{Zero}
为正的奇数时,输出:{Positive & odd}
为负的偶数时,输出:{Negative & even}
以此类推。
输出 的例子如下:
Input n: 22
{Positive & even}
Press any key to continue
Input n: -15
{Negative & odd}
Press any key to continue
Input n: 0
{Zero}
Press any key to continue
输出文字如下(请复制):
"Input n: "
"{Zero}\n"
"{Positive & even}\n"
"{Positive & odd}\n"
"{Negative & even}\n"
"{Negative & odd}\n"
代码如下:
// 从下一行开始复制。
#include <iostream.h>
void main(void) {
int n;
cout << "Input n: ";
cin >> n;
if(n==0){
cout << "{Zero}\n";
}
if(n>0 && n%2==0){
cout << "{Positive & even}\n";
}
if(n>0 && n%2!=0){
cout << "{Positive & odd}\n";
}
if(n<0 && n%2==0){
cout << "{Negative & even}\n";
}
if(n<0 && n%2!=0){
cout << "{Negative & odd}\n";
}
}
// 一直复制到上一行结束。
【Jitor 校验第 2 步】 // 复制上面的代码到VC++ 6.0,保存并运行。点击。
【第 3 步】实训总结
本次实训通过数的正负和奇偶的判断巩固了对条件分支的理解。
【Jitor 校验第 3 步】我已阅读实训总结 // 送分题。直接点击。
仍有疑问 ? 联系QQ 9429444(陈海云) : 返回首页 |