2014-09-20 54 views
1

我是C編程新手,在C編程中我無法掌握如何使用whileif函數。理解循環的問題

目前我有這個粗略的嘗試在開始我的程序。我目前收到錯誤消息'expected expression',其中有'if語句',但我相信代碼中有更多錯誤,因爲它不會執行我正在尋找的操作。

理想情況下,我想讓用戶繼續獲得相同的錯誤信息,直到他們從1-3正確輸入一個數字,然後只要他們輸入正確的數字,它就會存儲它並繼續前進到下一步。對不起,我在這方面很新,而且我一直對所閱讀的所有不同的教程感到困惑,因爲我正在閱讀循環和if語句。

任何幫助將不勝感激。

#include <stdio.h> 

int main(void) 
{ 
    printf("Welcome to the ROBOT GAME!\n" 
      "To help you visualize draw a three by three board on a piece of paper.\n" 
      "Please enter the column of your starting point.\n" 
      "Keep numbers in the range of one to three.\n"); 
    int a; 
    scanf("%i", &a); 
    while(a<1||a>3) 
    { 
     printf("Error: Sorry try again.\n" 
       "Please enter the column of your starting point.\n"); 
     int a; 
     scanf("%i", &a); 
     break; 

     if(a=>1 && a<=3) 
      printf("Please enter the row of your starting point. Keep numbers in the range of one to three.\n"); 

     int b; 
     scanf("%i", &b); 
    } 
} 
+2

它看起來像'if'語句需要打開和關閉大括號(即''''和'}')。否則,您*有條件*打印郵件,但*無條件*接受輸入。 – jww 2014-09-20 20:53:05

+0

總是會使用「break」。關掉你的電腦,拿一支鉛筆和一張紙,先畫一個流程圖。 – usr2564301 2014-09-20 20:56:13

+0

[C程序問題與循環]的可能重複(http://stackoverflow.com/questions/25945438/c-program-problems-with-loops) – hugomg 2014-09-20 20:59:52

回答

0

刪除突破

,並使用

if(a <= 1 && a <=3) 

,而不是

if(a =< 1 && a <=3) 
1

雖然你的程序是錯誤還是錯誤消息意味着你需要替換=>>= if語句的條件

if(a=>1 && a<=3) 

也就是說必須有

if(a >= 1 && a<=3) 
    ^^^^ 

的代碼可以像

#include <stdio.h> 

int main(void) 
{ 
    printf("Welcome to the ROBOT GAME!\n" 
      "To help you visualize draw a three by three board on a piece of paper.\n" 
      "Please enter the column of your starting point.\n" 
      "Keep numbers in the range of one to three.\n"); 
    int a; 

    scanf("%i", &a); 

    while (a < 1 || a > 3) 
    { 
     printf("Error: Sorry try again.\n" 
       "Please enter the column of your starting point.\n"); 

     scanf("%i", &a); 
    } 

    int b; 

    printf("Please enter the row of your starting point. Keep numbers in the range of one to three.\n"); 

    scanf("%i", &b); 

    while (b < 1 || b > 3) 
    { 
     printf("Error: Sorry try again.\n" 
       "Please enter the row of your starting point.\n"); 

     scanf("%i", &b); 
    } 

    return 0; 
} 
0

我爲了做什麼改變了你的源代碼,你想要,但我會建議你嘗試使用do {} while循環來重寫程序。

#include <stdio.h> 

int main(void) 
{ 
    printf("Welcome to the ROBOT GAME!\n" 
      "To help you visualize draw a three by three board on a piece of paper.\n" 
      "Please enter the column of your starting point.\n" 
      "Keep numbers in the range of one to three.\n"); 
    int a,b; 
    scanf("%i", &a); 
    while(a<1||a>3) 
    { 
     printf("Error: Sorry try again.\n" 
       "Please enter the column of your starting point.\n"); 
     scanf("%i", &a); 
    } 
    printf("Please enter the row of your starting point. Keep numbers in the range of one to three.\n"); 
    scanf("%i", &b); 
    while(b<1||b>3) 
    { 
     printf("Error: Sorry try again.\n" 
       "Please enter the row of your starting point.\n"); 
     scanf("%i", &b); 
    } 
}