我在C的第一个课程

时间:2011-03-13 23:02:24

标签: c

好的,所以我正在为twitter做一个小程序,基本上你要做的就是键入你有多少粉丝,你会收到回复。

我的问题是,当您输入关注者数量时,您只能收到< 50

的第一个回复

这是代码(也在http://pastie.org/1668357):

#include <stdio.h>

int followers;

int main() {


  {
  int followers;

  printf( "Please Enter How Many Twitter Followers You Have...\n" );
  scanf ( "%d" , &followers );
  getchar ();
}
  if ( followers > 50 ) {
   printf ("You may be new to Twitter or rarely use it, try tweeting about more topics.\n" );
   getchar ();

}

  else if ( followers < 100 ) {
   printf ("Reply: Not bad but keep Tweeting.\n" );
   getchar ();
}

  else if ( followers < 200 ) {
   printf ("Reply: People must be interested in your stuff, keep tweeting.\n" );
   getchar();
}

  else if ( followers < 500 ) {
   printf("Reply: Great, you must like Tweeting and other people must like your Tweets, Good job!\n" );
   getchar();
}

  else if ( followers < 1000 ) {
   printf ("Reply: Wow, that's a lot, send some to me @Cian_W\n" );
   getchar();
}

  else if ( followers < 1000000 ) {
   printf ("Reply: Jeez, are you famous? I'd say you like Tweeting\n" );
   getchar();
}

  else if ( followers != 1000000 ) {
   printf ("Reply: Holy crap, are you a celeb? Many people must like you.\n" );
   getchar();

}
getchar();
return 0;
}

由于 奇安

4 个答案:

答案 0 :(得分:4)

你输入了显然应该是:“if(followers&lt; 50)”as“if(followers&gt; 50)”。第8,9和14行也应删除。

编辑:由于问题现在包含代码(没有行号),因此应删除的部分如下:

{
    int followers;

    printf( "Please Enter How Many Twitter Followers You Have...\n" );
    scanf ( "%d" , &followers );
    getchar ();
}

您希望摆脱{}。您还希望摆脱followers定义的两个地方之一,例如:int followers;

我原先说过应该删除main中的一个,但实际上删除全局可能会更好。它将以任何一种方式工作 - 重点是确保您从用户读取的followers也在if语句中使用。对于这个微小的程序,使用局部变量和全局变量之间的区别并不重要,但从长远来看,学习避免全局变量会更好。

答案 1 :(得分:1)

删除第9行 - 您有两个名为followers不同变量 - 并将第15行更改为< 50

最后一个应该是else if ( followers >= 1000000 ) - >=而不是!=

答案 2 :(得分:1)

您将followers变量声明为全局变量,并在main()内的内部块中声明一个变量。当您致电scanf()时,您可能正在写入属于最内层范围的followers。一旦你击中那个结束支架,它就会超出范围。尝试摆脱全球追随者的数量,并摆脱对scanf()号召唤的支持。您还需要在第一个if语句中修复逻辑,以便进行相反的比较。

答案 3 :(得分:1)

你应该检查相反的方向。

if( x > 1000000)
{
}
else if(x > 10000)
{
}
else if(x > 50 )
{
}

或者您应该匹配确切的间隔

if(x<50)
{
}
else if((x>=50) && (x<1000))
{
}
else if((x>=1000) && (x<2000))
{
}