2017-02-12 46 views
-5

嗨,我的教官問我編寫了一個代碼,可以獲得一個學生的3個年級的平均值,並輸出一個特定的字符串,每個區間的平均值爲 (例如,如果AVG​​ b/w 70 cout < <「優秀\ n」;)但是有一個條件,在這種情況下,對於學生錯過三個測試中的一個來說,沒有問題 。我一直在調整 小時,我不知道如何創建兩個條件(兩個差異eqns>平均值(如果你錯過了一個測試,其他如果你不這樣做) 也不知道如何以創建循環,它永遠繼續,因爲在下面的語句我不>知道爲什麼笑感謝使用循環和if else語句的平均值

#include <iostream> 
using namespace std; 

int main() 
{ 
    double test1(0),test2(0),test3(0),AVG,AVG2,counter(1); 
    cout << " please write score of test1\n"; 
    cin >> test1; 

    cout << "Please enter test 2 grade\n"; 
    cin >> test2; 

    cout << "Please enter test 3 grade\n"; 
    cout << "if test 3 is not there please write in 0\n"; 
    cin >>test3; 

    cout << "test1 score:"<< test1<<"\n"; 
    cout << "test2 score:"<< test2<<"\n"; 
    cout << "test3 score:"<< test3<<"\n"; 

    AVG=(test1+test2+test3)/3; 
    AVG2=(test1+test2+test3)/2; 

    if (test3==0) 
    { 
     while (counter<=20) 
      cout <<" Average2 : "<<AVG2<<"\n"; 

     if (test3 <=0) 
      cout<<"Result (average of two tests):"; 

     if ((AVG2 <=100) && (AVG2>=70)) 
      cout<<"Excellent !\n"; 
     else 
      if ((AVG2 <70) && (AVG2>=50)) 
       cout << "Moderate !\n"; 
      else 
       if ((AVG2 <50) && (AVG2>0)) 
        cout << "Fail !\n"; 
       else 
        if ((AVG2 <0) || (AVG2>100)) 
         cout << "Error for avg2!\n"; 
     counter= counter +1; 
    } 
    else (!(test3==0)); 
    { 
     while (counter<=20) 
     { 
      cout<<"Average of three tests: "<<AVG<<"\n"; 
      if ((AVG <=100) && (AVG>=70)) 
       cout<<"Excellent !\n"; 
      else 
       if ((AVG <70) && (AVG>=50)) 
        cout << "Moderate !\n"; 
      else 
       if ((AVG <50) && (AVG>0)) 
        cout << "Fail !\n"; 
      else 
       if ((AVG <0) || (AVG>100)) 
        cout << "Error for avg1!\n"; 
      counter = counter +1; 
     } 

     return 0; 
    } 
} 
+1

'else' * what * ?? –

+0

if中的'while'循環持續到永遠,因爲你需要使用括號{} – peval27

+0

'counter'的含義是什麼?你能澄清你在做什麼嗎? – peval27

回答

0

在這裏和那裏你犯了一些錯誤,但不要擔心,沒關係。

你已經假設學生不會只參加test3,但它可能是test1或test2。

另一個要注意的是,您分配0的比分對於尚未taken.Again這可能產生一個問題,因爲它可能是學生出現,但得分0

我有測試完善了代碼並照顧了所有角落案例,並且它完全符合您的教練所需。

#include<iostream> 
using namespace std; 

int main() 
{ 
int avg,test1,test2,test3,left=0; 
cout<<"Enter the test scores.\n"; 
cout<<"If a student has not appeared in a test, please enter -1\n"; 
cout<<"\nTest 1 : "; 
cin>>test1; 
if(test1==-1) 
    left++; 

cout<<"\nTest 2 : "; 
cin>>test2; 
if(test2==-1) 
    left++; 

cout<<"\nTest 3 : "; 
cin>>test3; 
if(test3==-1) 
    left++; 

if(left>1) 
{ 
    cout<<"FAIL : The student has not appeared in more than one tests"; 
    return 0; 
} 
else if(left==1) 
{ 
    if(test1==-1) 
    { 
     avg=(test2+test3)/2; 

    } 
    else if(test2==-1) 
    { 
     avg=(test1+test3)/2; 
    } 
    else if(test3==-1) 
    { 
     avg=(test1+test2)/2; 
    } 
} 
else if(left==0) 
{ 
    avg=(test1+test2+test3)/3; 
} 

if(avg>=70 && avg<=100) 
{ 
    cout<<"Excellent !\n"; 
} 
else if(avg>=50 && avg<70) 
{ 
    cout<<"Moderate !\n"; 
} 
else if(avg>=0 && avg<50) 
{ 
    cout<<"Fail !\n"; 
} 
else 
{ 
    cout<<"Internal Computation Error"; 
    return 0; 
} 
return 0; 
} 
+0

謝謝你,你不知道你提供了多少幫助!我只在兩週前開始編程,並沒有完全掌握所有內容。再次感謝你,喬爾。 – maycarbon

+1

你能解釋一下新的代碼嗎?謝謝 – maycarbon

+0

好吧,讓我們看看有兩種情況,要麼學生參加所有的考試,要麼離開其中一項考試。但是也有一個隱藏的案例,如果學生離開了兩次以上的考試。沒有自然的直覺,我認爲學生在這種情況下會失敗,我以這種方式創建了程序。好的,現在讓我們進入程序。我用三個變量'test1','test2'和'test3'來存儲測試值。變量'left'來計算學生未採取的測試次數。 –

0

遇到一個無限循環:

while (counter<=20) 
    cout <<" Average2 : "<<AVG2<<"\n"; 

循環執行只有這行代碼永遠(即cout)。你需要在你想執行的代碼周圍添加括號在每個執行:

while (counter<=20) 
{ 
    // cout <<" Average2 : "<<AVG2<<"\n"; 
    // all you logic with if-else 

    counter = counter +1; // or counter++; 
} 

以這種方式所有{}之間的代碼被執行,包括計數器的增量。

+0

非常感謝,你的幫助非常感謝 – maycarbon

+0

順便說一句,你能幫我寫下這行嗎? – maycarbon

+0

else(!(test3 == 0 )編譯器說表達式結果未使用,我不明白它是什麼意思..再次感謝 – maycarbon