- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 C 语言编写了这段代码。它的基本功能是接受用户的输入(数字),并且根据用户选择的数字,它将运行一个 for
循环。 for 循环将要求用户根据其要求的语句输入更多数字。然后,代码将从用户那里获取这些输入并将其应用到公式中。
假设用户首先输入2。它应该做的是循环两次,并从第一次和第二次中取出总和并将它们加在一起。不幸的是,它不会这样做,只会返回循环中的第一个结果,并与第二个结果分开。
这是代码:
#include <stdio.h>
int main(){
int i = 0;
int robotMany = 0;
int enginePower = 0;
int resistance = 0;
int weight = 0;
int height = 0;
printf("Depending the numbers of robot that you enter.You will need to do the following\n");
printf("Engine power\nResistance\nWeight\nHeight\n");
printf("Enter the amount of robots\n");
scanf("%d",&robotMany);
printf("You chose %d robot\n", robotMany);
//Depending the user input, it will run # of times.
for(i = 0; i < robotMany; i++){
//for loop begins
printf("Enter the engine power.(1-20)\n");
scanf("%d", &enginePower);
printf("Enter the resistance.(1-3)\n");
scanf("%d", &resistance);
printf("Enter the weight of the robot\n");
scanf("%d", &weight);
printf("Enter the height of Robot.\n");
scanf("%d", &height);
int product;
//take the user inputs and apply it to the formual.
product = (enginePower + resistance) * (weight - height);
printf("This is your total power\n");
printf("%d\n", product);
}
return 0;
}
最佳答案
您存在一个基本逻辑错误,即未能保持每次循环迭代中计算的product
的运行总计。
但是,您遇到了一个更大的问题,即无法验证 scanf
(或用于用户输入的任何函数)的返回。验证每个用户输入至关重要,在使用 scanf
以避免在代码中调用未定义行为时更是如此。为什么?如果用户输入的杂散字符与格式字符串中的转换说明符不匹配,则会发生匹配失败,并从中提取字符>stdin
此时停止,将有问题的字符留在输入缓冲区中未读。在您的情况下,这将导致在程序的其余部分中每次调用 scanf
时都会出现相同的匹配失败。
例如,用户在输入 robotMany
时按下了 '2'
键,但意外点击了 'w'
键。不仅 robotMany
的输入失败,而且由于 'w'
留在 stdin
中未读,导致enginePower、阻力、重量和高度
的输入也失败。 (如果这种情况发生在循环内,您的循环可能会失去控制,直到手动终止该进程)
这就是为什么始终验证任何输入函数的返回至关重要,尤其是 scanf
系列函数。要捕获并处理任何输入失败(匹配失败、输入失败或手动生成的 EOF
),只需检查返回,例如
if (scanf ("%d", &robotMany) != 1) { /* VALIDATE EVERY USER INPUT! */
fputs ("error: invalid input, or user canceled - robotMany\n", stderr);
return 1;
}
关于你的逻辑错误。首先,您需要一个变量来跟踪循环每次行程中计算出的每个product
的总和。由于看起来这将是总功率,因此只需添加一个 totalpwr
变量并将其初始化为零(因为您已经很好地处理了所有变量),例如
int i = 0,
robotMany = 0,
enginePower = 0,
resistance = 0,
weight = 0,
height = 0,
totalpwr = 0; /* sum of product from each iteration */
现在,在循环中,您希望每次计算时将 product
添加到 totalpwr
中,例如
/* Depending the user input, it will run # of times. */
for (i = 0; i < robotMany; i++) {
...
/* take the user inputs and apply it to the formula. */
int product = (enginePower + resistance) * (weight - height);
totalpwr += product; /* add product to total power */
}
要输出totalpwr
,您将输出退出循环后的值,例如
...
}
/* you output the total power required outside the loop */
printf ("\ntotal power required: %d\n", totalpwr);
return 0;
}
将其完全放在一个示例中(不传递 product
计算的正确性或所需值的范围),您可以执行类似于以下操作的操作:
#include <stdio.h>
int main (void) {
int i = 0,
robotMany = 0,
enginePower = 0,
resistance = 0,
weight = 0,
height = 0,
totalpwr = 0; /* sum of product from each iteration */
/* you only need a single printf (fputs would do due to no conversion) */
printf ("Depending the numbers of robot that you enter, you will need "
"to enter:\n\n"
" Engine power | Resistance | Weight | Height\n\n"
"Enter the amount of robots: ");
if (scanf ("%d", &robotMany) != 1) { /* VALIDATE EVERY USER INPUT! */
fputs ("error: invalid input, or user canceled - robotMany\n", stderr);
return 1;
}
printf ("\nYou chose: %d robots\n\n", robotMany);
/* Depending the user input, it will run # of times. */
for (i = 0; i < robotMany; i++) {
printf ("Enter the engine power.(1-20): ");
if (scanf("%d", &enginePower) != 1) { /* validate! */
fputs ("error: invalid input - enginePower\n", stderr);
return 1;
}
printf ("Enter the resistance.(1-3): ");
if (scanf("%d", &resistance) != 1) { /* validate! */
fputs ("error: invalid input - resistance\n", stderr);
return 1;
}
printf ("Enter the weight of the robot: ");
if (scanf("%d", &weight) != 1) { /* validate! */
fputs ("error: invalid input - weight\n", stderr);
return 1;
}
printf ("Enter the height of Robot: ");
if (scanf("%d", &height) != 1) { /* validate! */
fputs ("error: invalid input - height\n", stderr);
return 1;
}
/* take the user inputs and apply it to the formula. */
int product = (enginePower + resistance) * (weight - height);
/* the following outputs the per-loop/per-robot product (you can remove) */
printf (" iteration %d product: %d\n\n", i+1, product);
totalpwr += product; /* add product to total power */
}
/* you output the total power required outside the loop */
printf ("\ntotal power required: %d\n", totalpwr);
return 0;
}
(注意:只需一个printf
即可输出多行输出,无需逐行重复调用。所有顺序字符串将由编译器连接)
示例使用/输出
$ ./bin/robotmany
Depending the numbers of robot that you enter, you will need to enter:
Engine power | Resistance | Weight | Height
Enter the amount of robots: 3
You chose: 3 robots
Enter the engine power.(1-20): 4
Enter the resistance.(1-3): 2
Enter the weight of the robot: 10
Enter the height of Robot: 31
iteration 1 product: -126
Enter the engine power.(1-20): 5
Enter the resistance.(1-3): 3
Enter the weight of the robot: 9
Enter the height of Robot: 31
iteration 2 product: -176
Enter the engine power.(1-20): 3
Enter the resistance.(1-3): 1
Enter the weight of the robot: 8
Enter the height of Robot: 31
iteration 3 product: -92
total power required: -394
您可能希望在重量
和高度
条目上添加约束,以防止出现负的所需功率结果 - 但这由您决定。检查一下,如果您还有其他问题,请告诉我。
关于c - 很难在 C 中将结果相加(For 循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57155221/
我是一名优秀的程序员,十分优秀!