簡體   English   中英

在 C 檢查每個元素的值並返回 true 或函數

[英]In C checking the value of every element and return true or function

有人可以幫助我或提供有關如何更正數組循環中的邏輯的想法

我的問題是我有學生測驗分數和完美測驗分數

我的目標是我想將學生測驗分數的每個元素與完美測驗分數進行比較,其中學生分數不能為負值或大於完美測驗分數,如果輸入為負值,我只想將其重新輸入到它所在的值不是負數或大於測驗分數,但我真的不知道該怎么做。

我還嘗試為quizScore創建函數並在那里輸入循環,如果測驗為負或大於,那么我將只調用quizScore函數,但它仍然失敗。

void initStudents() {
    printf("\nEnter number of students: ");
    scanf("%d", &studentCount);
    for(int i=0; i < studentCount; i++){
        printf("\n-------------------------------------------------------------------\n");
        printf("\n[Student %d of %d]\n", i + 1,studentCount);
        printf("Enter name of student %d: \n", i + 1);
        scanf("%s",studentNames[i]);
        for(int j = 0; j < qCount; j++){
        printf("\n[Quiz %d of %d] \n", j + 1, qCount);
        printf("Enter score for quiz %d: \n", j + 1);
        scanf("%d", &quiz[j]);
        if(quiz[j] < 0 || quiz[j] > qPerfect[j]) {
            quiz[j] = 0;
        }
        else {
            //no idea what to put here
        }
    }
    printf("\n-------------------------------------------------------------------\n");
    for(int j = 0; j < pCount; j++) {
        printf("\n[Project %d of %d]\n", j + 1, pCount);
        printf("Enter score for project %d: \n", j + 1);
        scanf("%d", &project[j]);
    }
    printf("\n-------------------------------------------------------------------\n");
    for(int j = 0; j < hmCount; j++) {
        printf("\n[Homework %d of %d]\n", j + 1, hmCount);
        printf("Enter score for homework %d: \n", j + 1);
        scanf("%d", &homework[j]);
    }       
}
//end of initstudents

循環直到有一個有效的輸入:

while (true) {
    printf("\n[Quiz %d of %d] \n", j+1, qCount);
    printf("Enter score for quiz %d: \n", j+1);
    scanf("%d",&quiz[j]);

    if (quiz[j] < 0 || quiz[j] > qPerfect[j]) {
        quiz[j] = 0;
    } else {
        break;
    }
}

你可以使用while

while (quiz[j] < 0 || quiz[j] > qPerfect[j]) {
    printf("Invalid score \n");
    printf("Enter score for quiz %d: \n", j + 1);
    scanf("%d", &quiz[j]);
}

暫無
暫無

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

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