簡體   English   中英

If else語句c程序中的多個條件

[英]Multiple conditions in an If else statement c program

我的程序應該基本上根據用戶輸入的邊長確定三角形的類型。
我在我的代碼中收到此錯誤消息,該代碼將進行測試以查看是否為斜角三角形

預期的';' 在'{'else之前((right!= left)||(right!= bottom)||(left!= bottom)){

碼:

else((right != left) || (right != bottom) || (left != bottom)){
                printf("This is a scalene triangle");
            }  

錯誤是說放一個; 就在最后一個條件之后,這對我來說沒有意義。 我試圖這樣做來測試它,但是它給了我錯誤的答案。

我假設這應該是一個if...else在這種情況下, ifelse之后, if需要一個額外的...

else if ((right != left) || (right != bottom) || (left != bottom))
{
    printf("This is a scalene triangle");
}

回應OP的評論...

從我正在閱讀的筆記中可以看出,格式是else的最后一個語句(如果只是其他語言),因此為什么我不使用else if而只是使用else

注釋在某種程度上是正確的-最后一條語句可以else語句(如果您有else則必須是最后一條語句,並且只能有一個)。

所以以下是有效的...

if (a == 1) {
   // Do this
} else {
   // Do that
}

但是下面是無效 ...

if (a == 1) {
   // Do this
} else {
   // Do that
} else {
   // Do other
}

else if允許您繼續對多個塊進行邏輯處理...如果需要,可以以else塊結束...

if (a == 1) {
   // Do this
} else if (b == 1) {
   // Do that
} else {
   // Do other
}

或不...

if (a == 1) {
   // Do this
} else if (b == 1) {
   // Do that
} else if (c == 1) {
   // Do other
}

else不能有條件語句,如果需要包括條件檢查,則應該為else if

必須如下更改

else if ((right != left) || (right != bottom) || (left != bottom)){
                printf("This is a scalene triangle");
            }  

暫無
暫無

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

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