帮我计算​​平均值 [英] Help me calculate avg

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

问题描述

我有一个关于计算平均值的问题,因为我认为不起作用。它表示输入的标记必须为0到100(包括0和100),或者提示用户重新输入,直到收到有效标记。用户输入标记-1以结束输入过程。然后该程序应显示:??



1.已处理了多少学生

2.所有学生的平均分数



提示:应该有两个嵌套循环来解决这个问题 - 一个在花药内部 - 用于验证用户输入的循环应该在内部循环中。



我尝试了什么:



I have a stuck about calculate average does not work as I think so. It says marks entered must be 0 to 100 (including 0 and 100), or the user is prompted to re-enter until a valid mark is received. The user enters a mark of -1 to end the input process. The program should then display: 

1. How many students have been processed
2. The average mark of all students

Hints: there should be two nested loops to solve this problem - one inside anther - the loop for validating user input should be in the inner one.

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practical_1_Q16_Calculate_Average
{
    class Program
    {
        static void Main(string[] args)
        {
            double total = 0;
            int count = 0;
            double score = 5;

            Console.WriteLine("Enter a score (0-100): ");
            score = double.Parse(Console.ReadLine());
            while (score < 0 || score > 100)
            {
                Console.WriteLine("Invalid, must be 0-100");
                score = double.Parse(Console.ReadLine());
                total += score;
                count++;
            }
            if (count == 0)
            {
                Console.WriteLine("No numbers were entered.");
            }
            else
            {
                Console.WriteLine("Average:" + total / count);
            }
            Console.WriteLine("");
            Console.WriteLine("Program finished");
            Console.ReadLine();
        }
    }
}

推荐答案

首先阅读说明,并注意提示



然后,我建议使用double.TryParse比使用double更好.Parse:如果输入了56个值然后输入错误一个和应用程序崩溃,你不会是一个快乐的兔子! TryParse告诉你这是一个错误的数字,而不是抛出异常并使程序崩溃,所以你可以告诉用户并继续前进。



然后,当你得到来自用户的值-1,你应该退出循环并显示结果。



当你从循环外的用户读取第一个值时,有多少你有样品吗?它真的是零吗?



最后,循环执行它包含的行只要条件是 true 。如果用户以有效值开头,它将永远不会进入你的循环...正如我所说,阅读提示...
Start by reading the instructions again, and paying attention to the "Hint"

Then, I'd suggest that double.TryParse would be a lot better to use than double.Parse: if you entered 56 values and then mistyped one and the app crashed, you wouldn't be a happy bunny! TryParse tells you it was a bad number instead of throwing an exception and crashing the program, so you can tell the user and move on.

Then, when you get the value "-1" from the user, you should exit the loop and show the results.

When you read the first value from the user outside your loop, how many samples have you got? is it really zero?

And finally, the while loop executes the lines it contains as long as the condition is true. If the user starts with a valid value, it will never enter your loop...as I said, read the "hint"...


你没有按照提示:你的程序没有两个嵌套循环,伪代码:



You didn't follow the hints: your program doesn't feature two nested loops, pseudo-code:

do 
{
  score = get_from_user();
  if ( score >= 0 && score <=100)
  {
    update_and_possibly_print_values();
  }
} while (score != -1);


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

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