- 雕是V,OWL是O,EAGLE爲E ...
- 甲
for
循環到輸入數據中每個觀鳥已收集。 - 在
for
循環內,一個do ... while
循環,用於輸入和處理由一個觀鳥者收集的數據。 - 在
do ... while
循環內有一個switch
聲明用於計算每種鳥類的卵數。當輸入x
時,將使用默認選項,該選項不執行任何操作。 do ... while
當爲某種類型的鳥輸入X時退出循環。- 的共計部分是罰款,每個代碼如下
OK,現在我的問題是,我似乎無法通過我的開關的情況下得到的。它提示我輸入第一個觀察者信息,當我輸入它時,它永遠不會移動到下一個觀察者。
給出的輸入數據是
3
E2 O1 V2 E1 O3 X0
V2 V1 O1 E3 O2 E1 X0
V2 E1 X
這裏是我到目前爲止的代碼:您需要每個開關情況後休息
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int totNrVultureEggs, totNrEagleEggs, totNrOwlEggs, nrEggs,
nrVultureEggs, nrEagleEggs, nrOwlEggs, nrBirdWatchers, nrEggsEntered;
char bird;
// initialize grand totals for number of eggs for each type of bird
cout << "How many bird watchers took part in the study?";
cin >> nrBirdWatchers;
// loop over number of bird watchers
for (int i = 0; i < nrBirdWatchers ;i++)
{
// initialize totals for number of eggs for each type of bird
// this bird watcher saw
nrVultureEggs = 0;
nrEagleEggs = 0;
nrOwlEggs = 0;
cout << "\nEnter data for bird watcher " << i + 1 << ":" << endl;
//loop over bird watchers
do{
cin >> bird >> nrEggs;
switch (bird)
{
case 'E':
case 'e':
nrEagleEggs = nrEagleEggs + nrEggs;
case 'O':
case 'o':
nrOwlEggs = nrOwlEggs + nrEggs;
case 'V':
case 'v':
nrVultureEggs = nrVultureEggs + nrEggs;
default :
nrBirdWatchers++;
break;
}
}while (i < nrBirdWatchers)
;
cout << "Bird watcher " << i + 1 << " saw " << nrVultureEggs;
cout << " vulture eggs, " << nrEagleEggs << " eagle eggs and ";
cout << nrOwlEggs << " owl eggs " << endl;
// increment grand totals for eggs
}
// display results
cout << "\nTotal number of vulture eggs: " << totNrVultureEggs;
cout << "\nTotal number of eagle eggs: " << totNrEagleEggs;
cout << "\nTotal number of owl eggs: " << totNrOwlEggs;
return 0;
}
你需要一個'break' – nullpotent
你忘了'打破;'在每個'情況:'聲明。這意味着只要ANY匹配,在該點以下的其他案例中的所有代碼都會被執行。 –
我添加了它,但它仍然不想工作...案例'E': 案例'e': nrEagleEggs = nrEagleEggs + nrEggs; 休息; 案例'O': 案例'o': nrOwlEggs = nrOwlEggs + nrEggs; 休息; 'V': 'v': nrVultureEggs = nrVultureEggs + nrEggs; 休息; 默認值: nrBirdWatchers ++; 休息; – Peanut