程序计算测验分数的平均值. [英] program calculating the mean of test scores.

查看:87
本文介绍了程序计算测验分数的平均值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在编写一个计算考试成绩平均值的程序.

用户必须输入介于1到100之间的整数,并且该整数为true时存储的数字为false时将忽略它而不计算它

这是我的代码,如果有另一种方法可以更正我.

hi guys am coding a program calculating the mean of test scores.

the user must be enter integer numbers between 1-100 and when that is true the number stored when it is false to ignore it and not calculate it

here is my code to correct for me if there is another way to do this.

#include <iostream>

using namespace std;

int main()
{
    int a,b,c,d;
    float mean,total;

    cout<<"please enter score between 1-100"<<endl;
    cin>>a;
    cout<<"please enter score between 1-100"<<endl;
    cin>>b;
    cout<<"please enter score between 1-100"<<endl;
    cin>>c;
    cout<<"please enter score between 1-100"<<endl;
    cin>>d;

    if(a<=100){
    cout<<"your score is = "<<a<<endl;

    }else{

    cout<<"ERROR INTEGER MUST BE BETWEEN 1-100 \n"<<a<<endl;

    }
    if(b<=100){
    cout<<"your score is = "<<b<<endl;

    }else{

    cout<<"ERROR INTEGER MUST BE BETWEEN 1-100 \n"<<b<<endl;

    }


if(c<=100){
    cout<<"your score is = "<<c<<endl;

    }else{

    cout<<"ERROR INTEGER MUST BE BETWEEN 1-100 \n"<<c<<endl;

    }

if(d<=100){
    cout<<"your score is = "<<d<<endl;

    }else{

    cout<<"ERROR INTEGER MUST BE BETWEEN 1-100 \n"<<d<<endl;

    }

    mean=(a+b+c+d)/4;
    cout<<"the mean number is "<<mean<<endl;
        return 0;
}

推荐答案

1)您应该立即学习模块化代码.如果发现重复重复执行类似的代码块,则应将其移入其自己的函数中.
1) You should learn to modularize your code right away. If you find that you are repeating a block of similar code more than once you should move it into its own function.
ValidateUserEntryValue(a);
ValidateUserEntryValue(b);
ValidateUserEntryValue(c);
ValidateUserEntryValue(d);

void ValidateUserEntryValue(int value)
{
   if( value <= 100 )
   {
      cout<< "your score is = "<<value<<endl;
   }
   else
   {
      cout<<"ERROR  INTEGER MUST BE BETWEEN 1-100 \n"<<value<<endl;
   }
}



2)您检查以确保输入的值在正确的范围内,但不能确保该值实际上是整数.我相信,如果用户输入的内容不会自动转换为整数,那么编写的程序将完全崩溃.为此,您应该使用 TryParse [



2) You do check to ensure that the value entered is within the correct range, but you do not ensure that the value is actually an integer. I believe your program as it is written will simply crash if the user enters something that doesn''t automatically convert to an integer. To do that you should run a make use of the TryParse[^] method provided for you by the dotNet framework.

3) At this point you just have to write the logic to calculate your mean while ignoring bad values. That part I''m leaving for you because you haven''t written anything to do that yet.

Cheers.


报价:

用户必须输入1-100之间的整数

the user must be enter integer numbers between 1-100

要求的检查不正确,例如

This requirement is NOT well checked, e.g.

报价:

if(a&== 100){

if(a<=100){

仅检查上限(例如,接受-10).应该是:

checks only the upper range (for instance -10 would be accepted). It should be:

if (a >= 1 && a <= 100)





报价:

mean =(a + b + c + d)/4;

mean=(a+b+c+d)/4;

假设一个或多个输入被拒绝(不在范围):那您为什么还继续使用它们?

相反,您应该计数收集多少有效输入,并且如果计数不是4,则

  • 仅使用有效输入来计算平均值.
  • 由于无效输入而中止计算.
  • Suppose one or more inputs are rejected (not in range): then why are you still using all of them?

    Instead you should count how many valid inputs are collected and, if the count is not 4 then either

    • Compute the mean value using only the valid inputs.
    • Abort computation because of invalid inputs.

    • 这篇关于程序计算测验分数的平均值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆