有人可以帮我解决这个在 Dev-C++ 上用 C 语言编程的练习吗?

发布于 2024-11-05 17:12:35 字数 667 浏览 1 评论 0原文

编写一个读取数字的程序,直到读取到数字 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

生活了然无味 2024-11-12 17:12:35

您的 if 语句永远不可能为真:

if ((num1>=-99) && (num1<=-10) && (num1>=10) && (num1<=99))

因为两个有效范围不重叠,并且您使用 && 来“组合”它们。

代替使用

if (((-99 <= num1) && (num1 <= -10)) || ((10 <= num1) && (num1 <= 99)))


我今天的可读性提示:请注意,我稍微调整了比较。范围比较更接近地反映了它们在代数符号中的外观:

(-99 <= num1 <= -10) or (10 <= num1 <= 99)

这是一件小事,但我发现它对于能够遵循此类测试的逻辑有很大的不同。只是不要陷入这样的编码陷阱:

if ((-99 <= num1 <= -10) || (10 <= num1 <= 99))

不幸的是,这在 C/C++ 中语法上是有效的,但不会给你你想要的结果。

Your if statement can never be true:

if ((num1>=-99) && (num1<=-10) && (num1>=10) && (num1<=99))

since the two valid ranges don't overlap and you're using && to 'combine' them.

Use

if (((-99 <= num1) && (num1 <= -10)) || ((10 <= num1) && (num1 <= 99)))

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:

(-99 <= num1 <= -10) or (10 <= num1 <= 99)

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:

if ((-99 <= num1 <= -10) || (10 <= num1 <= 99))

Which, unfortunately, is syntactically valid in C/C++ but will not give you the results you want.

GRAY°灰色天空 2024-11-12 17:12:35
(num1>=-99) && (num1<=-10) && (num1>=10) && (num1<=99)

一个数怎样才能满足所有这些条件呢?例如,(num1<=-10) && (num1>=10)想出一个可以满足这两个条件的数字吗?

(num1>=-99) && (num1<=-10) && (num1>=10) && (num1<=99)

How can a number satisfy all these conditions ? For instance, (num1<=-10) && (num1>=10) think of a number which can satisfy these two conditions ?

无所谓啦 2024-11-12 17:12:35

num1 可以同时为 <-10 和 >10 吗?

Can num1 be both <-10 and >10 ?

情话已封尘 2024-11-12 17:12:35

您的条件

(num1>=-99) && (num1<=-10) && (num1>=10) && (num1<=99)

永远不可能为true,因为没有数字可以同时为负数 (<=-10) 正数 (> =10)。

Your condition

(num1>=-99) && (num1<=-10) && (num1>=10) && (num1<=99)

can never be true, because no number can both be negative (<=-10) and positive (>=10).

天赋异禀 2024-11-12 17:12:35

你搞砸了两位数的检查:

你做了

(-99 <= n <= -10) AND (10 <= n <= 99)

你应该做的

(-99 <= n <= -10) OR (10 <= n <= 99)

(或者是 C 中的 ||

You messed up the check for two-digitness:

You did

(-99 <= n <= -10) AND (10 <= n <= 99)

You should have done

(-99 <= n <= -10) OR (10 <= n <= 99)

(Or is || in C)

风启觞 2024-11-12 17:12:35

问题是你的 if 语句。让我们尝试使用 2 个任意 2 位数字。 -42 和 42。

对于负 42:

   true            true           false        false

((num1>=-99) && (num1<=-10) && (num1>=10) && (num1<=99))

正 42:

   false          false           true         true

((num1>=-99) && (num1<=-10) && (num1>=10) && (num1<=99))

The problem is your if statement. Let's try it with 2 arbitrary 2 digit numbers. -42 and 42.

For negative 42:

   true            true           false        false

((num1>=-99) && (num1<=-10) && (num1>=10) && (num1<=99))

Positive 42:

   false          false           true         true

((num1>=-99) && (num1<=-10) && (num1>=10) && (num1<=99))
心清如水 2024-11-12 17:12:35

如果包含 math.h,则可以将其简化为:

if (fabs(num1) >= 10 && fabs(num1) <= 99){
    sum = sum + num1;
}

尽管我猜测 C++ 也支持 += 。

if you include math.h, you could simplify this to:

if (fabs(num1) >= 10 && fabs(num1) <= 99){
    sum = sum + num1;
}

Although I'm guessing C++ also has support for += as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文