Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# for循环的输出摘要,以便它输出摘要_C#_Console Application - Fatal编程技术网

C# for循环的输出摘要,以便它输出摘要

C# for循环的输出摘要,以便它输出摘要,c#,console-application,C#,Console Application,正在尝试从当前输出中获取摘要。例如,Hello:5,World:8,HelloWorld:20,integer:5。目前,它接受用户输入,例如1,10,1最小,10最大,并从for循环输出以下内容。不知道如何将输出放入数组,然后将其聚合 迄今为止的代码: Console.WriteLine("Please enter a range?"); string range = Console.ReadLine(); string first = range.Split

正在尝试从当前输出中获取摘要。例如,Hello:5,World:8,HelloWorld:20,integer:5。目前,它接受用户输入,例如1,10,1最小,10最大,并从for循环输出以下内容。不知道如何将输出放入数组,然后将其聚合

迄今为止的代码:

 Console.WriteLine("Please enter a range?");
        string range = Console.ReadLine();
        string first = range.Split(',')[0];
        string last = range.Split(',')[1];
        int min = Int32.Parse(first);
        int max = Int32.Parse(last);
        Console.WriteLine("");
        Console.WriteLine("Heres the Output");
        Console.WriteLine("");

        for (int i = min; i < max ; i++ )
        {
            bool hello = i % 3 == 0;
            bool world = i % 5 == 0;

            if (hello && world)
                Console.WriteLine("HelloWorld");

            else if (hello)
                Console.WriteLine("Hello");
            else if (world)
                Console.WriteLine("World");
            else
                Console.WriteLine(i);
        }

        string list = Console.ReadLine();
        Console.WriteLine(list);
Console.WriteLine(“请输入一个范围?”);
字符串范围=Console.ReadLine();
string first=range.Split(',')[0];
字符串last=range.Split(',')[1];
int min=Int32.Parse(第一);
int max=Int32.Parse(最后一个);
控制台。写线(“”);
Console.WriteLine(“这里是输出”);
控制台。写线(“”);
对于(int i=min;i
你所要做的就是使用模块化算术;如果
0,您能提供一些示例吗?例如,所需的
数组
是什么,比如说
[min=8,max=14]
输入?用户输入:1,10当前输出:1 2 hello 4 world hello 7 8 helloI想做什么:hello:3 world:1 integer:5在上面提供了一个输入示例1,10谢谢。让我明白这是唯一的办法吗?我的想法是以某种方式将for循环输出到一个数组中,然后在该数组中循环并聚合。我不懂模运算,可能需要仔细阅读。@RA19:loop在范围较宽时可能是一个错误的选择,例如,
100..100000000
@RA19:I已编辑了答案(提供了循环解决方案)。典型的解决方案是在您的案例中实现
IEnumerable
-
IEnumerable
,并使用Linq聚合data@RA19:Linq(Linq到代码中的对象)是对
IEnumerable
的查询,请参见
private static int CountDivs(int value, int divisor) {
  return value < 0 ? 0 : value / divisor + 1;
}

private static string Solution(int min, int max) {
  int hello = CountDivs(max - 1, 3) - CountDivs(min - 1, 3);
  int world = CountDivs(max - 1, 5) - CountDivs(min - 1, 5);
  int helloWorld = CountDivs(max - 1, 3 * 5) - CountDivs(min - 1, 3 * 5);
  // others: Inclusion–exclusion principle  
  int other = max - min - hello - world + helloWorld;

  return $"hello: {hello} world: {world} integer: {other}"; 
}

// Test (your example)
Console.WriteLine(Solution(1, 10));
// My comment [8..14) i.e. 8, 9, 10, 11, 12, 13
Console.WriteLine(Solution(8, 14));
hello: 3 world: 1 integer: 5
hello: 2 world: 1 integer: 3
using System.Linq;

...

private static IEnumerable<string> Generator(int min, int max) {
  // Your loop extracted: put "yield return ..." instead of "Console.WriteLine(...)"
  for (int i = min; i < max; ++i) {
    bool hello = i % 3 == 0;
    bool world = i % 5 == 0;

    if (hello && world) {
      // Or if you want return twice, just do it twice:
      // yield return "Hello";
      // yield return "World";

      yield return "HelloWorld";
    } 
    else if (hello)
      yield return "Hello";
    else if (world)
      yield return "World";
    else
      yield return "integer";
  }
}

...

var data = Generator(1, 10)
  .GroupBy(item => item)
  .OrderBy(chunk => chunk.Key)
  .Select(chunk => $"{chunk.Key}: {chunk.Count()}");

var result = string.Join(" ", data);

Console.WriteLine(result);
Hello: 3 integer: 5 World: 1
int min = 1;
int max = 10;

Dictionary<string, int> counts = new Dictionary<string, int>() {
  { "HelloWorld", 0},
  { "Hello", 0},
  { "World", 0},
  { "integer", 0},
};

foreach (var item in Generator(1, 10))
  counts[item] += 1;

string result = 
  $"Hello: {counts["Hello"]} World: {counts["World"]} integer: {counts["integer"]}";

Console.Write(result);
Hello: 3 World: 1 integer: 5