使用for循环打印最终列表(c#帮助)

使用for循环打印最终列表(c#帮助),c#,C#,因此,我必须尝试打印一组特定的人(值),我的输入是:10,2,2,3 输出应为1,3,7,9 然而,我最终得到:1,5,7 问题就在代码的末尾,但我有点困惑该怎么办 守则: //First Part - Ask user for how many friends there are Console.WriteLine("How many friends are there in total? [Must Be >= 1 and <= 100]"); string friend = C

因此,我必须尝试打印一组特定的人(值),我的输入是:10,2,2,3 输出应为1,3,7,9 然而,我最终得到:1,5,7 问题就在代码的末尾,但我有点困惑该怎么办

守则:

//First Part - Ask user for how many friends there are
Console.WriteLine("How many friends are there in total? [Must Be >= 1 and <= 100]");
string friend = Console.ReadLine();
int AmountOfPeople = Convert.ToInt32(friend);

//Check if the input meets requirements
if (AmountOfPeople < 1 | AmountOfPeople > 100)
{
    Console.WriteLine("This is not valid, please restart program.");
}

//Make a list to provide the user with the number of friends coming
List<int> Friends = new List<int>();
int count = AmountOfPeople;
for(int i=1;i<=count;i++)
{
    Friends.Add(i);
}

//Another blank list
List<int> Final = new List<int>();

//Second Part- Ask user how many rounds of removal there will be
Console.WriteLine("How many rounds of removal will there be? [Between 1 and 10]");
string rounds = Console.ReadLine();
int TotalRounds = Convert.ToInt32(rounds);
if (TotalRounds < 1 | TotalRounds > 10)
{
    Console.WriteLine("This is not valid, please restart program.");
}

for (int i = 0; i < TotalRounds; i++)
{
    Console.WriteLine("What multiple of numbers would you like to remove? [eg. 2,3,4...]");
    string multiple = Console.ReadLine();
    int pick = Convert.ToInt32(multiple);

    foreach (int j in Friends.Reverse<int>())
    {
        if (j % pick == 0)
        {
           Friends.Remove(j);
        }
    }                    
}

Console.WriteLine("");
Console.WriteLine("Here are the guests");
Console.WriteLine("");

foreach (int i in Friends)
{      
    Console.WriteLine(i);       
}

Console.ReadLine();
//第一部分-询问用户有多少朋友

Console.WriteLine(“总共有多少个朋友?[必须>=1和首先,我同意@Steve的观点,即输出已经是正确的。我认为这是一个更简洁的解决方案,尽管仍然需要做一些工作(转义条件、解析的异常处理,或者更好地使用tryParse)

while(true)
{
WriteLine(“总共有多少朋友?”);
int-amountOfPeople=int.Parse(Console.ReadLine());
如果(人数<1 | |人数>100)
{
Console.WriteLine(“输入无效”);
继续;
}
var friendsList=Enumerable.Range(1,amountOfPeople.ToList();
控制台。WriteLine(“将有多少轮删除?”);
int totalRounds=int.Parse(Console.ReadLine());
如果(人数<1 | |人数>10)
{
Console.WriteLine(“输入无效”);
继续;
}
对于(int i=0;ix%pick==0);
}
Console.WriteLine(“\r\n他们是来宾\r\n”);
friendsList.ForEach(Console.WriteLine);
}
惯例也很酷。

  • 类名称和方法名称的PASCALCING
  • 骆驼套法 参数和局部变量

首先,使用
|
而不是
|
——如果第一个条件已经通过,则不会检查第二个条件。代码未编译!!第一步,将输入和输入验证与处理分开。块注释是一种代码气味,指示什么应该是单独的函数。第二步,单元测试processing、 学习单元测试永远不会太早。第三步,使用调试器逐步完成失败的测试。对于输入10,2,2,3,结果1,5,7是正确的。此代码将首先删除所有偶数,在第一个循环结束时留下1,3,5,7,9,然后删除所有3(3和9)的倍数因此,输出是1,5,7。要获得预期结果,最后一个输入应该是5,而此时它可能看起来并不重要,另一个小注释:局部变量(如代码中的
AmountOfPeople
)应该以非大写字母开头,否则对其他人来说它看起来像类名的属性-这是.Net约定。我可以建议重构
控制台.Readline
并对方法调用
GetInput(int minValid,int maxValid)进行输入验证吗
,然后身体看起来更干净了。wcipeg.com/problem/ccc14s1@Dec是的,我认为这也是正确的,但我认为我误解了这个问题,现在有点困惑应该改变什么。我同意@Elucius,我会编辑。Salty-好吧,既然你说你现在认为你得到的输出是正确的,你可以什么都不改变,或者你可以我会尝试接受评论/我的答案中的一些概念。它们都很有帮助。@Dec我正在解决的2和3的每一个倍数都将被删除,这使得我所做的是正确的,但我认为实际的问题是从列表中删除每2个值,然后是每3个值。这将导致1,3,7,9。我还将链接放到了显示实际问题的注释如下:well@Dec除了使用linq之外,还有一些好的建议。
int.Parse
如果条目不是数字,将导致异常。请改用
int.TryParse
方法。
        while (true)
        {
            Console.WriteLine("How many friends are there in total?");
            int amountOfPeople = int.Parse(Console.ReadLine());

            if (amountOfPeople < 1 || amountOfPeople > 100)
            {
                Console.WriteLine("Input is not valid");
                continue;
            }

            var friendsList = Enumerable.Range(1, amountOfPeople).ToList();
            Console.WriteLine("How many rounds of removal will there be?");
            int totalRounds = int.Parse(Console.ReadLine());

            if (amountOfPeople < 1 || amountOfPeople > 10)
            {
                Console.WriteLine("Input is not valid");
                continue;
            }

            for (int i = 0; i < totalRounds; i++)
            {
                Console.WriteLine("Multiples to remove?");
                int pick = int.Parse(Console.ReadLine());
                friendsList.RemoveAll(x => x % pick == 0);
            }

            Console.WriteLine("\r\nHere are the guests\r\n");
            friendsList.ForEach(Console.WriteLine);                
        }