c# - 如何拯救这个任务?
问题描述
我想知道如何做或编写一个代码,它将打印出没有人得到 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: ");
}
}
}
}
解决方案
从您的问题下方的评论中只是一个疯狂的猜测:
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 分时才打印内容)
推荐阅读
- c# - Entity Framework Core - 使用接口作为参数的表达式树
- react-native - ScrollView 中的数组映射不呈现
- java - Spring Boot 集成 SFTP 和谷歌云存储
- android - 如何从文件夹运行 ./gradlew 命令?
- c++ - 找不到 Visual Studio 2008 (Platform Toolset = 'v90') 的构建工具
- python - Python。构建exe
- c# - 如何删除数组中的所有匹配项并将剩余部分向左移动?
- react-native - 导航组件/屏幕是否必须卸载?
- javascript - 如何使用java脚本设置数据表超出第一页的行数据?
- c# - 如果 JSON 字符串具有未闭合的 html 标签,如何使用 AntiXss