简单的输出问题 [英] Simple output problem

查看:58
本文介绍了简单的输出问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须解决以下问题:编写一个接受两个

整数的程序,并确定第二个是否是一个因子(是否可以整除

into)第一。这是我到目前为止的代码。

#include< stdio.h>

#include< stdlib.h>


int main()

{

int a; //股息

int b; // divisor

int c;

c == 0;


printf("< ------保理机器-------> \ n");

printf(" \\\
Please输入一个整数后跟\ n");

printf(一个空格,然后是一个可能的因子integer.\ n);

printf("例如:[ab]是正确的格式。\ n");

scanf("%d%d",& a,& b);

c == a / b;

if (a%b == 0)

{

printf(" \ n%d是因子%d \ n,b,a);

printf(\ n另一个因素是%d \ n,c);


}


其他

{

printf(\ n%d不是%d \ n因子,b,a);

}

系统(PAUSE);

返回0;

}


为什么第二个printf中的%d没有输出变量值

c?

这应该是另一个因素。

I have to solve the following problem:Write a program that accepts two
integers, and determines if the second is a factor (is evenly divisible
into) the first. Here is the code i have so far.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a; //dividend
int b; //divisor
int c;
c==0;

printf("<------The Factoring Machine------->\n");
printf("\nPlease enter an integer followed by\n");
printf("a space and then the possible factor integer.\n");
printf("For exampe: [a b] is the correct format.\n");
scanf("%d%d", &a, &b);
c == a/b;
if(a%b==0)
{
printf("\n %d is a factor of %d\n", b, a);
printf("\nThe other factor is %d\n", c);

}

else
{
printf("\n%d is not a factor of %d\n",b,a);
}
system("PAUSE");
return 0;
}

why is the %d in the second printf not outputting the value of variable
c?
This should be the other factor.

推荐答案

问题在于读取c == a / b;的行。使用两个等于

比较(布尔条件)。它要求c等于a / b?,

返回零或一个为真或假,但不保存这个

值任何地方。


您可能要做的是c =(int)a / b;这将取a / b的

值并将其放入变量c中。另外,因为a / b是
返回一个double,所以告诉编译器

将它强制转换为整数可能是一个好主意,因为c是一个整数。没有(int),

你可能会得到编译器警告,具体取决于编译器。


祝你好运。

RadiationX写道:
The problem is in the line that reads "c == a/b;". Using two equals is
a comparison (boolean condition). It''s asking "is c equal to a/b?",
returning either a zero or one for true or false, but doesn''t save this
value anywhere.

What you probably meant to do is "c = (int) a/b;" which will take the
value of a/b and place it into the variable c. Also, because a/b is
returning a double, it''s probably a good idea to tell the compiler to
cast it back to an integer because c is an integer. Without the (int),
you might get a compiler warning depending on the compiler.

Good luck.
RadiationX wrote:
我必须解决以下问题:编写一个接受两个
整数的程序,并确定第二个是否是一个因子(可以被分成第一个)。这是我到目前为止的代码。

#include< stdio.h>
#include< stdlib.h>

int main()
{
int a; // dividend
int b; // divisor
int c;
c == 0;

printf("< ------ The Factoring Machine -------> ; \ n");
printf(\ n请输入一个整数,后跟\ n);
printf(一个空格,然后是可能的因子integer.\ n; );
printf(例如:[ab]是正确的格式。\ n);
scanf("%d%d",& a,& b);
c == a / b;

if(a%b == 0)
{/> printf(" \ n%d是因素%d \ n",b,a);
printf(\ n其他因素是%d \ n,c);

}


{/> printf(\ n%d不是%d \ n的因素,b,a);
}
系统( PAUSE);
返回0;
}
为什么第二个printf中的%d没有输出变量的值
c?这应该是另一个因素。
I have to solve the following problem:Write a program that accepts two
integers, and determines if the second is a factor (is evenly divisible
into) the first. Here is the code i have so far.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a; //dividend
int b; //divisor
int c;
c==0;

printf("<------The Factoring Machine------->\n");
printf("\nPlease enter an integer followed by\n");
printf("a space and then the possible factor integer.\n");
printf("For exampe: [a b] is the correct format.\n");
scanf("%d%d", &a, &b);
c == a/b;
if(a%b==0)
{
printf("\n %d is a factor of %d\n", b, a);
printf("\nThe other factor is %d\n", c);

}

else
{
printf("\n%d is not a factor of %d\n",b,a);
}
system("PAUSE");
return 0;
}

why is the %d in the second printf not outputting the value of variable
c?
This should be the other factor.






我刚刚重新调试,你在行中有同样的问题" c == 0; 。

无需初始化c,但如果你愿意,你可以使用

" c = 0;"。


作为提醒,双倍等于==用于比较,而单个=用于比较。

用于设置变量等于另一个变量或等式。当你把b $ b混合起来时,它可能是一个难以找到的bug,因为编译器

不会抱怨因为它是有效的C语法。

I also just reallized, you have the same problem in the line "c==0; ".
There is no need to initialize c, but if you wanted to, you would use
"c=0;".

As a reminder, double equal "==" is for comparison, while single "=" is
for setting a variable equal to another variable or equation. When you
mix this up, it can be a difficult bug to find because the compiler
isn''t going to complain because it is valid C syntax.


修复了它。谢谢..

that fixed it. thanks..


这篇关于简单的输出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆