簡體   English   中英

如何停止無限循環?

[英]How to stop the infinite loop?

我正在編寫一個程序,該程序將計算三角形或正方形的面積,然后提示用戶是否要計算另一個。 我已經讓代碼工作到它會計算任一形狀的面積,但不會繼續執行其余的代碼。 例如選擇正方形,計算面積,然后返回正方形邊的提示。 我假設它再次永遠循環,但不知道如何阻止循環無休止地進行。

這是我的代碼:

 #include<stdio.h>
 #include<math.h>
 
 int main(void)
 
 {
     float sq_side, tri_base, tri_height, Area;
     char shape, cont = 'Y';
     
     printf("Please select the type of shape you desire to calculate the area for:\n");
     printf("                                                                     \n");
     printf("   Square = S                             Triangle = T               \n");
     printf("   -------                                    x                      \n");
     printf("   :     :                                   x x                     \n");
     printf("   :     :                                  x   x                    \n");
     printf("   -------                                 xxxxxxx                   \n");
     printf("                                                                     \n");
     printf("Please select either S or T:");
     scanf("%c", &shape);
     while (cont != 'n' && cont != 'N')
     
        if (shape == 'S' || shape == 's')
        {
            printf("What is the length of the sides of the square?:\n");
            scanf("%f", &sq_side);
         
            Area = pow(sq_side,2);
         
            printf("The area of the square is %.2f.\n", Area);
        }   
         
        else if (shape == 'T' || shape == 't')
        {
            printf("What is the length of the base of the triangle?:\n");
            scanf("%f", &tri_base);
            printf("What is the height of the triangle?:\n");
            scanf("%f", &tri_height);
            
            Area = 0.5 * tri_base * tri_height;
            
            printf("The area of the triangle is %.2f.\n", Area);
        }   
        
        else 
        {
        printf("Error:  You have select an incorrect option.");
        }
        
        printf("Do you wish to calculate a new shape?");
        fflush(stdin);
        scanf("%c", &cont);
        
     return(0);
     
 }
 

你缺少花括號。 結果是只有 if 語句(包括 else ifs 鏈)實際上在循環體中。 printf (和更高版本)不是這個復合語句的一部分。

 while (cont != 'n' && cont != 'N')
 {
    if (shape == 'S' || shape == 's')
    {
        printf("What is the length of the sides of the square?:\n");
        scanf("%f", &sq_side);

        Area = pow(sq_side,2);

        printf("The area of the square is %.2f.\n", Area);
    }   

    else if (shape == 'T' || shape == 't')
    {
        printf("What is the length of the base of the triangle?:\n");
        scanf("%f", &tri_base);
        printf("What is the height of the triangle?:\n");
        scanf("%f", &tri_height);

        Area = 0.5 * tri_base * tri_height;

        printf("The area of the triangle is %.2f.\n", Area);
    }   

    else 
    {
    printf("Error:  You have select an incorrect option.");
    }

    printf("Do you wish to calculate a new shape?");
    fflush(stdin);
    scanf("%c", &cont);
}   

while 循環中沒有括號供您使用。 因此,不會調用 if elseif else 塊之外的代碼。

目前您的代碼轉換為

while (cont != 'n' && cont != 'N')
{
    if (shape == 'S' || shape == 's')
    {}
    else if (shape == 'T' || shape == 't')
    {}   
    else 
    {}
}

printf("Do you wish to calculate a new shape?");
fflush(stdin);
scanf("%c", &cont);

當你想要的時候

while (cont != 'n' && cont != 'N')
{
    if (shape == 'S' || shape == 's')
    {}
    else if (shape == 'T' || shape == 't')
    {}   
    else 
    {}

    printf("Do you wish to calculate a new shape?");
    fflush(stdin);
    scanf("%c", &cont);
}

請記住,如果您不在控制結構中包含大括號,它只會調用下一條語句,在您的情況下,它是一系列嵌套的 if-else if 語句。

希望它有幫助 - Val

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM