2014-10-26 77 views
0

我无法选择第二个任务。如果我运行第一项任务,没有问题,我会得到结果。但是我在第二和第三个任务中遇到了一些问题。即使我选择了第二个或第三个任务,程序也会要求我输入一个整数,用于第一个任务。这里是我的代码:无法运行第二个任务

#include <stdio.h> 
main(void) 
{ 

int n; 
int length, row = 0, column = 0, space;  // Variables of first program 
int top, bot, a, b; 
int top2, bot2, a2, b2; 

printf("Please select the task: "); 
scanf("%d", &n); 

if(n = 1) 
{ 
    printf("Please enter an integer: ");   // Program asks the length 
    scanf("%d", &length); 
    row = 0; 
    column = 0; 
    while(row < length)        // While loop for newline 
    { 
     while(column < length)      // While loop for stars 
     { 
      printf("*"); 
      column++; 
     } 
     printf("\n");        
     column = 0;         // Resetting the value of column variable 
      while(column < row+1)     // While loop for space 
      { 
       printf(" "); 
       column++; 
      } 
      column = 0; 
       row++; 
    } 
} 
else if(n = 2) 
{ 
    printf("Please enter the length of top:"); 
     scanf("%d", &top); 
    printf("Please enter the length of bottom:"); 
     scanf("%d", &bot); 

     a = top; 
     b = top; 

    while(a <= bot) 
    { 
     while(b <= a+2) 
     { 
      printf("*"); 
      b++; 
     } 
     b = top; 
     printf("\n"); 
     a++; 
    } 
} 
    else 
{ 
    printf("Please enter the length of top:"); 
     scanf("%d", &top2); 
    printf("Please enter the length of bottom:"); 
     scanf("%d", &bot2); 

     a2 = top2; 
     b2 = top2; 

    while(a2 >= bot2) 
    { 
     while(b2 >= a2) 
     { 
      printf("*"); 
      b2--; 
     } 
     b2 = top2; 
     printf("\n"); 
     a2--; 
    } 
} 
return 0; 
} 
+4

'如果(N = 1)'是不是做你认为它是。在编译器中打开警告。 – 2014-10-26 22:03:55

回答

3
if(n = 1) 

=是赋值运算符。 要比较,请使用==

你正在做什么是n设为1,并含蓄地检查是否值不同于0。所以你总是进入,如果分支,从来没有别的之一。