期望的输出:
输入值来查找因子? 10
数字10的因子为1,2,5和10
数字10有4个因子
再试一次y / n?
当前输出:
输入值来查找因子? 10
数字10的因子为1和10
数字10的因子为2和10
数字10的因子为5和10
数字10的因子为10和10
数字10的因子为-1,因此为10
数字10的因子为-2和10
数字10的因子为-5和10
数字10有-10和10因为它是因素
我目前的守则
#include <stdio.h> // printf, scanf, getchar
#include <stdlib.h> // system
int list_mult(int value);
int main()
{
int input_value;
char again;
do {
printf("Input value to find factors for ? ");
scanf("%d", &input_value);
list_mult(input_value);
printf("\nTry again y/n ? ");
scanf(" %c", &again);
} while (again == 'y');
}
int list_mult(value) {
int i;
int count = 0;
printf("\nThe number %d has", value);
for (i = 1; i <= (value / 2); i++)
{
if (value%i == 0)
{
printf(" %d,", i);
count++;
}
}
printf(" and %d as it factors", value);
printf("\nThe number %d has %d factors", value, count);
return(count + 1);
}
问题:
1st:do-while循环(再次)不工作
第二名:&#34;数字10有1,2,5和10作为其因素&#34; 。不知道如何展示&#34;我&#34;用逗号分隔。
第3名:上次打印声明(计数)不起作用
编辑:以上所有问题都已解决。以上代码在CodeBlock上完美运行
我现在遇到的问题是我使用CodeBlock并且我的代码完美无缺。但是当我在学校使用Microsoft Studio(Iam只允许在学校使用MS)时,MS会在下面显示错误
C2065&#39;值&#39;:未声明的标识符 - 第29行
C2365&#39; list_mult&#39;:重新定义;以前的定义是&#39;功能&#39; - 29号线
C2448&#39; list_mult&#39;:函数式初始化器似乎是一个函数定义
Edit2:解决了编译器错误。现在一切都很有效。 谢谢
答案 0 :(得分:1)
在第一次扫描到&amp;再次之后,你可以再放一行。我必须在我的系统上收到一个回车。
if (again == '\n') scanf("%c", &again);
对于第二个问题,您可以显示如下逗号:
printf("\nThe number %d has ", value);
for (i=1; i<=(value/2); i++) {
if(value%i==0) {
printf("%d", i);
putchar(',');
count++;
}
}
printf(" and %d as its factors.", value);
进行逗号格式化的另一种方法是创建值数组
int factors[1000];
并预先填充所有内容,然后遍历这些值以获得所需的格式化输出。例如,您将知道不打印最终的逗号,因为您在此时具有因子计数,以及预填充数组中的所有因子。
The number 10 has 1,2,5, and 10 as its factors. The number 10 has 3 factors Try again y/n ?