C# 任务的问题。编写一个程序,确定并打印输入的数字中可被3整除的数字

C# 任务的问题。编写一个程序,确定并打印输入的数字中可被3整除的数字,c#,C#,我对这项任务有意见。首先,这里有一些任务指南。 程序输入:从标准输入中加载一个自然数,先加载n个自然数,然后在新行中加载n个自然数。 程序输出:所有可被3整除的加载数字按顺序打印在标准输出上。 在单独的行中打印每个数字。 例如,我输入了数字: 100 99 3 4 6 每个数字必须在新行中 当我按enter键时,程序必须显示哪些数字可以被3整除。例如: 99 3 6 这是我的密码 string a; int n; while (true) {

我对这项任务有意见。首先,这里有一些任务指南。 程序输入:从标准输入中加载一个自然数,先加载n个自然数,然后在新行中加载n个自然数。 程序输出:所有可被3整除的加载数字按顺序打印在标准输出上。 在单独的行中打印每个数字。 例如,我输入了数字:

100    
99    
3    
4    
6
每个数字必须在新行中

当我按enter键时,程序必须显示哪些数字可以被3整除。例如:

99    
3    
6
这是我的密码

string a;
int n;
while (true)
{
    a = Console.ReadLine();
    if (a == string.Empty)
        break;

    n = Convert.ToInt32(a);
    {
        if (n % 3 == 0)
            Console.WriteLine(n);
    }
}

问题是,当我输入一个可被3整除的数字时,程序会立即打印它,而不是等待我输入完数字,然后才打印可被3整除的数字。

您需要重新构造程序,首先获取所有输入,然后处理它们

您可以这样做:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        // storage for all numbers
        var numbers = new List<int>();

        while (true)
        {
            var a = Console.ReadLine();
            if (a == string.Empty)
                break;

            // error handling to avoid crashing if non integers are given
            try
            {
                // adding all numbers in case you want to do other things with them 
                numbers.Add(Convert.ToInt32(a));
            }
            catch
            {
                Console.WriteLine($"Not a number: {a}");
            }
        }

        // process the collected numbers
        foreach (var n in numbers)
        {
            if (n % 3 == 0)
                Console.WriteLine(n);
        }
    }
}


正如评论中所建议的:这将无法完成给定的任务,您需要读取一个数字,然后读取尽可能多的输入(因此,如果为true,则无需读取任何输入:…)。

显示以下内容也许您可以找到有用的内容:

    static void Main()
    {
        var output = string.Empty;

        if (int.TryParse(Console.ReadLine(), out int iterations))
        {
            while (iterations-- > 0) // or iterations > 0 and if number pass then increase the iterations
            {
                if (int.TryParse(Console.ReadLine(), out int number) /*&& number != 0*/ && number % 3 == 0)
                {
                    output += $"{Environment.NewLine}{number}";
                    // iterations--;
                }
            }
        }

        Console.WriteLine(output);
    }
对于那些他们认为不应该添加字符串的人:

    static void Main()
    {
        var output = new StringBuilder(Environment.NewLine);

        if (int.TryParse(Console.ReadLine(), out int iterations))
        {
            while (iterations-- > 0) // or iterations > 0 and if number pass then increase the iterations
            {
                if (int.TryParse(Console.ReadLine(), out int number) /*&& number != 0*/ && number % 3 == 0)
                {
                    output.AppendLine($"{Environment.NewLine}{number}");
                    //iterations--;
                }
            }

            Console.WriteLine(output);
        }
    }
和解决方案列表:

    static void Main()
    {
        if (int.TryParse(Console.ReadLine(), out int iterations))
        {
            //var output = new List<int>();
            var length = iterations;
            var output = new int[length];

            while (iterations > 0) // or iterations > 0 and if number pass then increase the iterations
            {
                if (int.TryParse(Console.ReadLine(), out int number) /*&& number != 0*/ && number % 3 == 0)
                {
                    //outpu.Add(number);
                    output[length - iterations] = number;
                    iterations--;
                }
            }

            Console.WriteLine(string.Join(Environment.NewLine, output));
        }
    }
static void Main()
{
if(int.TryParse(Console.ReadLine(),out int迭代))
{
//var输出=新列表();
变量长度=迭代次数;
var输出=新整数[长度];
while(iterations>0)//或iterations>0,如果数字通过,则增加迭代次数
{
if(int.TryParse(Console.ReadLine(),out int number)/*&&number!=0*/&&number%3==0)
{
//输出。添加(编号);
输出[长度-迭代]=数量;
迭代--;
}
}
Console.WriteLine(string.Join(Environment.NewLine,output));
}
}

将所有数字添加到列表中。当您输入一个非数字时,处理列表并打印其中的%3==0。使用可分割的数字和换行符生成一个字符串,在程序结束时打印该字符串?旁注:
if(a==string.Empty)
-最好使用
string.IsNullOrEmpty(a)
string.IsNullOrWhitespace(a)
检查此项。另请注意:“从标准输入中首先加载一个自然数n,然后加载n个自然数”-您没有实现粗体部分。好的,因为这显然是一门课程:您是否可以使用
列表
?您是否熟悉数组,例如
int[]
?如果“解析失败,它仍然会减少迭代次数。”。。。不一定是错的。要求中没有提到这一点。仅供考虑…“不应添加字符串”-这一点很好。有人告诉我,这是一个过时的“优化”,在今天并不重要。我必须承认,在有人向我证明这是一个问题之前,我已经有一段时间没有深入研究过这个问题并使用concat了。当然,对于少量的字符串,根本不知道如何学习porpouses。
    static void Main()
    {
        if (int.TryParse(Console.ReadLine(), out int iterations))
        {
            //var output = new List<int>();
            var length = iterations;
            var output = new int[length];

            while (iterations > 0) // or iterations > 0 and if number pass then increase the iterations
            {
                if (int.TryParse(Console.ReadLine(), out int number) /*&& number != 0*/ && number % 3 == 0)
                {
                    //outpu.Add(number);
                    output[length - iterations] = number;
                    iterations--;
                }
            }

            Console.WriteLine(string.Join(Environment.NewLine, output));
        }
    }