簡體   English   中英

我怎樣才能打電話給我的 function 讓這個程序計算成績?

[英]How can I call my function to make this program work to calculate grades?

#include <iostream>

using namespace std;
int grade_calculation (int grade_input){
    if ( grade_input >= 100 && grade_input <= 90){
        cout << 'A';
    } else if (grade_input >= 89 && grade_input <= 80){
        cout << 'B';
    } else if (grade_input >= 79 && grade_input <= 70){
        cout << 'C';
    } else if (grade_input >= 69 && grade_input <= 60){
        cout << 'D';
    } else if (grade_input >= 59 && grade_input <= 0){
        cout << 'F';
    } else {
        cout << "NOT VALID.";
    }
}

int main()
{
    int score = 100;
    int grade_input;
    cout << "Enter a grade: ";
    cin >> grade_input;
    while (grade_input != score){
        cout << "Enter a new grade: ";
        cin >> grade_input;
        grade_calculation(grade_input);
        
    } cout << grade_input;
    return 0;
}

如何正確使用while循環向用戶詢問字母然后output相應的等級? 除了使用“grade_calculation (grade_input)”之外,還有其他調用 function 的方法嗎?

if ( grade_input >= 100 && grade_input <= 90)

這意味着:如果等級高於 100 且等級低於 90 沒有滿足這個條件的等級。 你可能想要:

if ( grade_input <= 100 && grade_input >= 90)

即:如果 grade 低於或等於 100 且 grade 等於或高於 90

然后,以類似的方式更改所有其他條件。

暫無
暫無

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

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