2014-10-26 57 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。所以你總是進入,如果分支,從來沒有別的之一。

相關問題