這個問題在這里已經有了答案: 鏈接多個大于/小于運算子 6 個答案 昨天關門。
如何do while在我的代碼中打破回圈?程式應該回圈,直到用戶從類別中輸入一個有效的年份。
當我輸入有效輸入時,printf即使它是要停止的,它仍然會出現。
下面是代碼:
#include <stdio.h>
int main()
{
float year;
do {
printf("Please input your year of birth :");
scanf("%f", &year);
if ((year >= 1946) && (year <= 1964))
printf("You belong to the baby boomer generation \n");
else if ((year >= 1965) && (year <= 1980))
printf("You belong to the Generation X \n");
else if ((year >= 1981) && (year <= 1996))
printf("You belong to the Millenials/Generation Y \n");
else if ((year >= 1997) && (year <= 2012))
printf("You belong to the Zoomers/Generation Z \n");
} while(1946 > year < 2012);
}
uj5u.com熱心網友回復:
(1946 > year < 2012)
應該
(year > 1946 && year < 2012)
break;如果你想在某個地方打破它,應該使用它
if(year == 2000)
break;
uj5u.com熱心網友回復:
你的while情況不像你預期的那樣作業。對于計算機,1946 > year < 2012是(1946 > year) < 2021;1946 > year將是0or 1,所以最后你有0 < 2021(or 1 < 2021),這總是正確的,并且回圈不會停止。
嘗試將其更改為 eg 1946 > year && year < 2012。
有關詳細資訊,請參閱https://stackoverflow.com/a/6961728/5471218。
uj5u.com熱心網友回復:
添加一個 else 條件來中斷回圈。
if ((year >= 1946) && (year <= 1964))
printf("You belong to the baby boomer generation \n");
else if ((year >= 1965) && (year <= 1980))
printf("You belong to the Generation X \n");
else if ((year >= 1981) && (year <= 1996))
printf("You belong to the Millenials/Generation Y \n");
else if ((year >= 1997) && (year <= 2012))
printf("You belong to the Zoomers/Generation Z \n");
else
break;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/424450.html
