简体   繁体   English

在 C 检查每个元素的值并返回 true 或函数

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

Can someone help me or give idea on how can I correct my logic in the array loop有人可以帮助我或提供有关如何更正数组循环中的逻辑的想法

My Problem is I have student quiz scores and perfect quiz scores我的问题是我有学生测验分数和完美测验分数

My Goal is I want to compare student quiz scores every element to the Perfect quiz scores, where Student scores cannot be negative value or greater than the perfect quiz scores, and if it is inputted negative I just want to reinput it to the value where it is not negative or greater than the quiz scores, but I really have no idea how to do it.我的目标是我想将学生测验分数的每个元素与完美测验分数进行比较,其中学生分数不能为负值或大于完美测验分数,如果输入为负值,我只想将其重新输入到它所在的值不是负数或大于测验分数,但我真的不知道该怎么做。

Also I tried creating function for quizScore and input the loop there and if the quiz is either negative or greater than, then I'll just call the quizScore function but it still fails.我还尝试为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

Loop until there's a valid input:循环直到有一个有效的输入:

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;
    }
}

You can do it using while你可以使用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.

相关问题 为什么此.every()函数返回true然后返回false? - Why does this .every() function return true and then false? 空数组返回true? 给定一个整数数组,如果每个元素都是 1 或 4,则返回 true - Empty array returns true? Given an array of ints, return true if every element is a 1 or a 4 将相同的函数应用于C中数组中的每个元素 - Applying same function to every element in an array in C 如何用C中的值“删除”数组中的每个元素 - How to “delete” every element in array by value in C PHP检查数组值true - PHP checking for array value true PHP检查数组中是否存在item,然后返回值-转换为函数 - PHP checking if item exists in array and then return the value - convert to a function 如果数组中的所有值都为真(字符串)并且其中一个值为假(字符串),则如何返回 boolean true 停止使用 Javascript 检查 - How to return a boolean true if all the values in an array are true (strings) and if one of the value is false(string) stop checking using Javascript Array.prototype.every 在测试数组中的空值有长度时返回 true? - Array.prototype.every return true when testing empty value in an array has length? 检查数组中的每个元素是否满足特定要求 - Checking every element in an array for a specific requirement 检查 lisp 中数组的每个元素 - Checking an array's every element in lisp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM