所以我必须尝试打印一组特定的人(值),我的输入是: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();
答案 0 :(得分:0)
首先,我同意@Steve的说法,输出已经正确。这是一个我认为有点整洁的解决方案,虽然仍然需要一点工作(转义条件,解析的异常处理,或更好地使用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);
}
会议也很酷。