首页 > 解决方案 > 如何拯救这个任务?

问题描述

我想知道如何做或编写一个代码,它将打印出没有人得到 20 分,并且它必须编写多行。一切正常,除了if (is20 == false). 如何解决?

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            double[,] points = new double[50, 5];
            Random r = new Random();
            for (int k = 0; k < 50; k++)
            {
                for (int j = 0; j < 5; j++)
                {
                    points[k, j] = r.NextDouble() * 5 + 15.5;
                    if (points[k, j] > 20) points[k, j] = 20;
                    Console.Write("{0:f1}", points[k, j]);
                    Console.Write(" ");
                }

                Console.WriteLine();
            }

            bestEstimated(points);
            Console.ReadLine();
        }

        public static void bestEstimated(double[,] points)
        {
            bool is20 = false;
            for (int line = 0; line < 50; line++)
            {    
                for (int column = 0; column < 5; column++)
                {
                    if (points[line, column] == 20)
                    {
                        Console.WriteLine("20 points got: " + line + " competitor");
                        is20 = true;
                        break;
                    }    
                }    
            }

            if (is20 == false)
            {
                Console.WriteLine("No one got 20 points: ");
            }
        }
    }
}

标签: c#console-application

解决方案


从您的问题下方的评论中只是一个疯狂的猜测:

public static void bestEstimated(double[,] points)
{
    var not20Points = new List<int>();
    for (int line = 0; line < 50; line++)
    {    
        bool is20 = false;
        for (int column = 0; column < 5; column++)
        {
            if (points[line, column] == 20)
            {
                Console.WriteLine("20 points got: " + line + " competitor");
                is20 = true;
                break;
            }
        }

        if (is20 == false)
        {
            Console.WriteLine("competitor" + line + " didnt get 20 points"); //also can print it here if ya want...
            not20Points.Add(line);
        }
    }

    if (not20Points.Count == 50)
    {
        Console.WriteLine("No one got 20 points");
    }
    else
    {
        Console.WriteLine("Those lines did get 20 points: " + string.Join(",", Enumerable.Range(0, 50).Except(not20Points)));
        Console.WriteLine("Those lines didnt get 20 points: " + string.Join(",", not20Points));
    }
}

(更新了我的版本,仅在至少一列有 20 分时才打印内容)


推荐阅读