如何在C中使用w来读取数字直到-1,并计算用户输入的偶数和奇数? [英] How to wap in C to read a number until -1 is encountered and count how many even and odd number entered by user?
本文介绍了如何在C中使用w来读取数字直到-1,并计算用户输入的偶数和奇数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C编程,控制语句章节。
我尝试过:
in C programming ,control statements chapter.
What I have tried:
/*I have done*/
int num,even=0,odd=0;
do{printf("enter number")
scanf("%d",&num);
if(num÷2==0)
even+=1;
else odd+=1;
printf("even number %d",even);
printf("odd number %d",odd);
推荐答案
学会正确缩进代码,显示其结构,有助于阅读和理解。它还有助于发现结构错误。
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
/*I have done*/
int num,even=0,odd=0;
do{
printf("enter number") // ; is missing here
scanf("%d",&num);
if(num÷2==0)
even+=1;
else
odd+=1;
// } is missing here
// end of loop is missing here
printf("even number %d",even);
printf("odd number %d",odd);
专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。
Notepad ++主页 [ ^ ]
ultraedit [ ^ ]
请注意,您的代码将计数-1作为输入值。
Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Note that your code will count -1 as an input value.
这应该可以解决问题:
This should do the trick:
int num = 0, even = 0, odd = 0;
do
{
printf( "enter number (-1 exits):" );
scanf( "%d", &num );
if ( num != -1 )
{
if ( num & 0x01 ) odd++;
else even++;
};
} while ( num != -1 );
printf( "even number %d", even );
printf( "odd number %d", odd );
我希望能帮到你。
-Doug
I Hope that helps you.
-Doug
首先你需要一些< a href =http://www.cplusplus.com/doc/tutorial/control/>关于do-while循环的教程。
生成的代码可能如下所示:
At first you need some tutorial about the do-while loop.
The resulting code could look like this:
int count = 0;
do {
count++;
if( ... )
break;//quick leave loop
}while( num!= -1;
这篇关于如何在C中使用w来读取数字直到-1,并计算用户输入的偶数和奇数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文