要求找最大值,而且程序只处理奇数。
#include<stdio.h>
int main(void)
{
int cnt = 0, n;
int max , x;
printf("The maximum value will be computed.\n");
printf("How many values do you want to enter ?\n");
scanf("%d",&n);
while(n <= 0 ){
printf("\nEEror: Positive integer reguired.\n\n");
printf("How many numbers do you wish to enter?");
scanf("%d",&n);
}
if(n == 1)
printf("\nEnter %d real number:\n",n);
else
printf("\nEnter %d real numbers:\n",n);
scanf("%d",&x);
while(x % 2 == 0){ /*integers should be odd numbers.*/
printf("please input odd numbers!\n");
printf("Input integers:\n");
scanf("%d",&x);
}
max = x;
while(++cnt < n){
scanf("%d",&x);
if(max < x)
max = x;
}
printf("\nMaximum value : %d\n",max);
getch();
return 0;
}
程序运行时,如果开始输入的第一个数字是偶数,可以显示出”please input odd numbers!“;一旦输入奇数以后,再输入偶数,就不再受x % 2 == 0的限制。
例如运行时:
The maximum value will be computed.
How many values do you want to enter ?
3
Enter 3 real numbers:
4
please input odd numbers!
Input integers:
3
6
8
Maximum value : 8
请帮忙修改程序。谢谢!
[此贴子已经被作者于2006-11-14 12:35:50编辑过]