有人可以帮我解决这个在 Dev-C++ 上用 C 语言编程的练习吗?
编写一个读取数字的程序,直到读取到数字 999,然后该过程停止读取。因此使用 do while。然后显示导入到计算机中的两位数(正数和负数)的总和以及导入到计算机中的数字总数。
当我插入数字 999 时,该过程停止。效果很好。 我确信 if 命令是错误的,因为它没有将任何数字相加。 我运行它,打印出两个数字的和为 0。
int num1, a, sum;
a = 0;
sum = 0;
do
{
printf("give number\n");
scanf("%d", &num1);
a = a + 1;
if ((num1>=-99) && (num1<=-10) && (num1>=10) && (num1<=99))
{
sum = sum + num1;
}
printf("the sum of the two digit numbers is %d\n", sum);
}
while (num1 != 999);
printf("the sum of the two digit numbers is %d\n", athr);
printf("the number of numbers that were inserted overall is %d\n", a);
Write a program that reads numbers until you read the number 999 then the process stops reading. Therefore use do while. Then display the sum of two digit numbers, both positive and negative that were imported in the computer and the number of numbers that were imported to the computer overall.
When I insert the number 999 the process stops. That works fine.
I'm sure that the if command is wrong cause it doesn't add any numbers in sum.
I run it and it prints out that the sum of the two digit numbers is 0.
int num1, a, sum;
a = 0;
sum = 0;
do
{
printf("give number\n");
scanf("%d", &num1);
a = a + 1;
if ((num1>=-99) && (num1<=-10) && (num1>=10) && (num1<=99))
{
sum = sum + num1;
}
printf("the sum of the two digit numbers is %d\n", sum);
}
while (num1 != 999);
printf("the sum of the two digit numbers is %d\n", athr);
printf("the number of numbers that were inserted overall is %d\n", a);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您的
if
语句永远不可能为真:因为两个有效范围不重叠,并且您使用
&&
来“组合”它们。代替使用
。
我今天的可读性提示:请注意,我稍微调整了比较。范围比较更接近地反映了它们在代数符号中的外观:
这是一件小事,但我发现它对于能够遵循此类测试的逻辑有很大的不同。只是不要陷入这样的编码陷阱:
不幸的是,这在 C/C++ 中语法上是有效的,但不会给你你想要的结果。
Your
if
statement can never be true:since the two valid ranges don't overlap and you're using
&&
to 'combine' them.Use
instead.
My readability tip of they day: note that I slightly rejiggered the comparisons. The range comparisons mirror a little more closely how they might look in algebraic notation:
It's a small thing, but I find it makes a big difference to being able to follow the logic of these kinds of tests. Just don't fall into the trap of coding this:
Which, unfortunately, is syntactically valid in C/C++ but will not give you the results you want.
一个数怎样才能满足所有这些条件呢?例如,
(num1<=-10) && (num1>=10)
想出一个可以满足这两个条件的数字吗?How can a number satisfy all these conditions ? For instance,
(num1<=-10) && (num1>=10)
think of a number which can satisfy these two conditions ?num1
可以同时为 <-10 和 >10 吗?Can
num1
be both <-10 and >10 ?您的条件
永远不可能为
true
,因为没有数字可以同时为负数 (<=-10
) 和 正数 (> =10
)。Your condition
can never be
true
, because no number can both be negative (<=-10
) and positive (>=10
).你搞砸了两位数的检查:
你做了
你应该做的
(或者是 C 中的
||
)You messed up the check for two-digitness:
You did
You should have done
(Or is
||
in C)问题是你的 if 语句。让我们尝试使用 2 个任意 2 位数字。 -42 和 42。
对于负 42:
正 42:
The problem is your if statement. Let's try it with 2 arbitrary 2 digit numbers. -42 and 42.
For negative 42:
Positive 42:
如果包含 math.h,则可以将其简化为:
尽管我猜测 C++ 也支持 += 。
if you include math.h, you could simplify this to:
Although I'm guessing C++ also has support for += as well.