簡體   English   中英

for或while循環內的if語句

[英]if statement inside for or while loop

我有循環不工作

它只顯示第一個提示輸入1個學生信息

或以太跳過循環並在其下進行所有操作

你能幫忙嗎

int x = 1;

    while (x <= 10) {
        ++x;
        if (x = 1) {
            cout << "enter 1 student info";
            cin >> name >> first >> second >> final>>id;
        } else if (x = 2) {
            cout << "enter 2 student info";
            cin >> name1 >> first1 >> second1 >> final1 >> id1;
        } else if (x = 3) {
            cout << "enter 3 student info";
            cin >> name2 >> first2 >> second2 >> final2 >> id2;
        } else if (x = 4) {
            cout << "enter 4 student info";
            cin >> name3 >> first3 >> second3 >> final3 >> id3;
        }
    }

...

   for (int x = 1; x <= 9; x++) {
            if (x = 1) {
                cout << "enter 1 student info";
                cin >> name >> first >> second >> final>>id;
            }
            if (x = 2) {
                cout << "enter 2 student info";
                cin >> name1 >> first1 >> second1 >> final1 >> id1;
            }
            if (x = 3) {
                cout << "enter 3 student info";
                cin >> name2 >> first2 >> second2 >> final2 >> id2;
            }
            if (x = 4) {
                cout << "enter 4 student info";
                cin >> name3 >> first3 >> second3 >> final3 >> id3;
            }
        }

如果您使用= (賦值運算符)而不是== (等於運算符),那么我只是向您展示第一個錯誤

int x=1;

while (x<=10)
{++x;
    if(x==1)

有關更多信息,請參見cpp參考或wikipidia。

暫無
暫無

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

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