C 如何打印用户输入内容的次数?“X中的Y”;";
例如,假设我想计算出有多少用户可以接收SSC 如何打印用户输入内容的次数?“X中的Y”;";,c,C,例如,假设我想计算出有多少用户可以接收SS int age = 0; printf("Enter you all's ages, when finished, type - 1\n"); while (age != -1) { scanf("%d", &age); if (age >= 65) printf("X out of Y meet SS' age requirements") } 其中,X是输入一个数字的用户数>=65,Y是输入的除-1以
int age = 0;
printf("Enter you all's ages, when finished, type - 1\n");
while (age != -1) {
scanf("%d", &age);
if (age >= 65)
printf("X out of Y meet SS' age requirements")
}
其中,X
是输入一个数字的用户数>=65
,Y
是输入的除-1
以外的数字总数。我该怎么做
我也注意到了我的代码中的一个缺陷。不是在第一个用户输入并且
年龄>=65
后打印最后一条打印语句,而是在程序完成后打印。(打印-1
后)您需要一个计数器来计算有多少人通过了年龄限制
所以你需要声明一个新变量,比如说int counter,
有打印行的地方,应该是计数器++
我不懂C,所以我无法为您编写代码:)让它成为一个do while循环,这样它就不会添加-1I,结果只是从-1开始,而不是从0开始,希望我的快捷方式不会受到反对。
int age = 0;
int over = 0;
int total = 0;
printf("Enter you all's ages, when finished, type -1\n");
while (age != -1){
scanf("%d", &age);
if (age >= 65){
over++;
}
total++;
printf("%d out of %d meet SS' age requirements", over, total);
}