C#:调整程序以计算大于或等于50的分数

C#:调整程序以计算大于或等于50的分数,c#,count,C#,Count,我的代码是用户需要输入介于之间的标记,然后如果标记不在0和100之间,它必须再次询问,我得到了它的工作,但现在的问题是:调整上述程序,也计算大于或等于50的标记数 我如何做到这一点,我已尝试将count声明为整数0,但我不确定如何实现 为了实现这一点 这是我的密码: static void Main(string[] args) { int total = 0; for (int x = 1; x <= 5; x++) { Console.Write

我的代码是用户需要输入介于之间的标记,然后如果标记不在0和100之间,它必须再次询问,我得到了它的工作,但现在的问题是:调整上述程序,也计算大于或等于50的标记数

我如何做到这一点,我已尝试将count声明为整数0,但我不确定如何实现 为了实现这一点

这是我的密码:

static void Main(string[] args)
{
    int total = 0;
    for (int x = 1; x <= 5; x++)
    {
        Console.WriteLine("Enter your mark");
        int mark = int.Parse(Console.ReadLine());
        if (mark > 100 || mark < 0)
        {
            Console.WriteLine("Invalid mark,Enter your mark again");
            int newmark = int.Parse(Console.ReadLine());
            mark = newmark;
        }
        total += mark;
    }
    Console.WriteLine("sum = " + total);
    double average = (total / 5) * 1.00;
    Console.WriteLine("average = " + average);
    Console.ReadLine();
}
static void Main(字符串[]args)
{
int-total=0;
对于(整数x=1;x100 | |标记<0)
{
Console.WriteLine(“无效标记,再次输入您的标记”);
int newmark=int.Parse(Console.ReadLine());
马克=纽马克;
}
总数+=分数;
}
Console.WriteLine(“总和=”+总计);
双平均=(总计/5)*1.00;
Console.WriteLine(“average=“+average”);
Console.ReadLine();
}
静态void Main(字符串[]args)
{
int-total=0;
int GT50计数=0;
对于(整数x=1;x100 | |标记<0)
{
Console.WriteLine(“无效标记,再次输入您的标记”);
int newmark=int.Parse(Console.ReadLine());
马克=纽马克;
}
总数+=分数;
如果(标记>=50)
{
gt50Count++;
}
}
Console.WriteLine(“总和=”+总计);
双平均=(总计/5)*1.00;
Console.WriteLine(“average=“+average”);
Console.WriteLine(“大于或等于50 count=“+gt50Count”);
Console.ReadLine();
}
静态void Main(字符串[]args)
{
int-total=0;
int GT50计数=0;
对于(整数x=1;x100 | |标记<0)
{
Console.WriteLine(“无效标记,再次输入您的标记”);
int newmark=int.Parse(Console.ReadLine());
马克=纽马克;
}
总数+=分数;
如果(标记>=50)
{
gt50Count++;
}
}
Console.WriteLine(“总和=”+总计);
双平均=(总计/5)*1.00;
Console.WriteLine(“average=“+average”);
Console.WriteLine(“大于或等于50 count=“+gt50Count”);
Console.ReadLine();
}

我会将检查标记是否无效的方式更改为while循环。仅通过该if检查,如果用户连续插入无效值,代码将接受它

static void Main(string[] args)
{
    int total = 0;
    int marksAbove50Count = 0;
    for (int x = 1; x <= 5; x++)
    {
        Console.WriteLine("Enter your mark");
        int mark = int.Parse(Console.ReadLine());
        while (mark > 100 || mark < 0)
        {
            Console.WriteLine("Invalid mark,Enter your mark again");
            int newmark = int.Parse(Console.ReadLine());
            mark = newmark;
        }
        total += mark;
        if(mark >= 50) marksAbove50Count++;
    }
    Console.WriteLine("sum = " + total);
    double average = (total / 5) * 1.00;
    Console.WriteLine("average = " + average);
    Console.WriteLine("Marks above 50 count: " + marksAbove50Count);
    Console.ReadLine();
}
static void Main(字符串[]args)
{
int-total=0;
int marksAbove50Count=0;
对于(整数x=1;x100 | |标记<0)
{
Console.WriteLine(“无效标记,再次输入您的标记”);
int newmark=int.Parse(Console.ReadLine());
马克=纽马克;
}
总数+=分数;
如果(标记>=50)标记超过50计数++;
}
Console.WriteLine(“总和=”+总计);
双平均=(总计/5)*1.00;
Console.WriteLine(“average=“+average”);
控制台写入线(“50计数以上的标记:+marksAbove50Count”);
Console.ReadLine();
}

我会将检查标记是否无效的方式更改为while循环。仅通过该if检查,如果用户连续插入无效值,代码将接受它

static void Main(string[] args)
{
    int total = 0;
    int marksAbove50Count = 0;
    for (int x = 1; x <= 5; x++)
    {
        Console.WriteLine("Enter your mark");
        int mark = int.Parse(Console.ReadLine());
        while (mark > 100 || mark < 0)
        {
            Console.WriteLine("Invalid mark,Enter your mark again");
            int newmark = int.Parse(Console.ReadLine());
            mark = newmark;
        }
        total += mark;
        if(mark >= 50) marksAbove50Count++;
    }
    Console.WriteLine("sum = " + total);
    double average = (total / 5) * 1.00;
    Console.WriteLine("average = " + average);
    Console.WriteLine("Marks above 50 count: " + marksAbove50Count);
    Console.ReadLine();
}
static void Main(字符串[]args)
{
int-total=0;
int marksAbove50Count=0;
对于(整数x=1;x100 | |标记<0)
{
Console.WriteLine(“无效标记,再次输入您的标记”);
int newmark=int.Parse(Console.ReadLine());
马克=纽马克;
}
总数+=分数;
如果(标记>=50)标记超过50计数++;
}
Console.WriteLine(“总和=”+总计);
双平均=(总计/5)*1.00;
Console.WriteLine(“average=“+average”);
控制台写入线(“50计数以上的标记:+marksAbove50Count”);
Console.ReadLine();
}
首先,我建议提取一种方法:不要将所有内容都塞进一个
Main

   private static int readMark() {
     Console.WriteLine("Enter your mark");

     int result = 0;

     while (true) 
       if (!int.TryParse(Console.ReadLine(), out result))
         Console.WriteLine("Incorrect syntax, enter your mark again");
       else if (result < 0 || result > 100)
         Console.WriteLine("Mark should be in [0..100] range, enter your mark again");   
       else
         return result;
   }
但是我们可以为/
foreach
循环设置一个好的

   static void Main(string[] args) {
     ...
     double average = marks.Average();
     int sum = marks.Sum();
     int countGreaterThan50 = marks.Count(item => item > 50);
   static void Main(string[] args) {
     ...
     int total = 0;
     countGreaterThan50 = 0;

     for (int i = 0; i < marks.Length; ++i) {
       total += marks[i];

       if (marks[i] > 50) 
         countGreaterThan50 += 1;
     }      

     // (double) total - be careful with integer division:
     // 91 / 5 == 18 when 91.0 / 5 == 18.2  
     double average = ((double) total) / marks.Length;
static void Main(字符串[]args){
...
int-total=0;
计数大于50=0;
对于(int i=0;i50)
计数大于50+=1;
}      
//(双精度)总计-小心整数除法:
//当91.0/5==18.2时,91/5==18
双倍平均=((双倍)总数)/分数。长度;
首先,我建议提取一种方法:不要将所有内容都塞进一个
Main

   private static int readMark() {
     Console.WriteLine("Enter your mark");

     int result = 0;

     while (true) 
       if (!int.TryParse(Console.ReadLine(), out result))
         Console.WriteLine("Incorrect syntax, enter your mark again");
       else if (result < 0 || result > 100)
         Console.WriteLine("Mark should be in [0..100] range, enter your mark again");   
       else
         return result;
   }
但是我们可以为
/
foreach
循环设置一个好的

   static void Main(string[] args) {
     ...
     double average = marks.Average();
     int sum = marks.Sum();
     int countGreaterThan50 = marks.Count(item => item > 50);
   static void Main(string[] args) {
     ...
     int total = 0;
     countGreaterThan50 = 0;

     for (int i = 0; i < marks.Length; ++i) {
       total += marks[i];

       if (marks[i] > 50) 
         countGreaterThan50 += 1;
     }      

     // (double) total - be careful with integer division:
     // 91 / 5 == 18 when 91.0 / 5 == 18.2  
     double average = ((double) total) / marks.Length;
static void Main(字符串[]args){
...
int-total=0;
计数大于50=0;
对于(int i=0;i50)
计数大于50+=1;
}      
//(双精度)总计-小心整数除法:
//当91.0/5==18.2时,91/5==18
双倍平均=((双倍)总数)/分数。长度;

如果(标记>=50){++gt50;}
如何将超过50的标记数显示回用户?构建如下字符串:
“超过50的标记数=”+gt50;
然后将其写入控制台。无关:不要使用幻数。如果你想获得5分以上的分数,你必须在代码中更改2位。我遇到的大多数评论员都会告诉你从0开始索引,结束条件x<5(但在99%的情况下,这只是一个品味问题)最后:你应该在除法之前将
total
转换为double,而不是与1f相乘:
if(mark>=50){++gt50;}
?如何将超过50的标记数显示回用户?构建如下字符串:
“超过50的标记数=”+gt50;
然后将其写入控制台。无关:不要使用幻数。如果你想获得5分以上的分数,你必须在代码中更改2位。我遇到的大多数评论员都会告诉你从0开始索引,结束条件x<5(但在99%的情况下,这只是一个品味问题)最后一点:你应该在之前将
total
转换为double