Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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# 如何从用户那里获取多选的正确值?_C# - Fatal编程技术网

C# 如何从用户那里获取多选的正确值?

C# 如何从用户那里获取多选的正确值?,c#,C#,当用户输入正确的值(1,2,3…)时,我希望程序更进一步 Console.WriteLine(“选择要下注的钱:1欧元,2欧元,3欧元,4欧元,5欧元,10欧元,15欧元,”+ "20€, 30€, 50€, 100€"); //int y=0; 而(!int.TryParse(Console.ReadLine(),out int y)| | y!=1 | | y!=2) //||y!=3 | | y!=4 | | y!=5 | | y!=10 | | y!=15 | | y!=20 //||y

当用户输入正确的值(1,2,3…)时,我希望程序更进一步

Console.WriteLine(“选择要下注的钱:1欧元,2欧元,3欧元,4欧元,5欧元,10欧元,15欧元,”+
"20€, 30€, 50€, 100€");
//int y=0;
而(!int.TryParse(Console.ReadLine(),out int y)| | y!=1 | | y!=2)
//||y!=3 | | y!=4 | | y!=5 | | y!=10 | | y!=15 | | y!=20

//||y!=30 | | y!=50 | | y!=100 | | y为此使用Hashset,您将拥有并调用Contains(y)来检查它是否正确,如下所示:

Console.OutputEncoding = System.Text.Encoding.UTF8;
HashSet<int> allowedNumbers = new HashSet<int>(new int[] { 1, 2, 3, 4, 5, 6, 8, 9, 10, 15, 20, 30, 50, 100 });
string allowedNumbersString = string.Join(", ", allowedNumbers.Select(n => n + "€"));

Console.WriteLine("Choose money to bet: " + allowedNumbersString);
while (!int.TryParse(Console.ReadLine(), out int y) || !allowedNumbers.Contains(y))
{
    Console.WriteLine("Choose correct money to bet: " + allowedNumbersString);
}
Console.outpunecoding=System.Text.Encoding.UTF8;
HashSet allowedNumbers=新的HashSet(新的int[]{1,2,3,4,5,6,8,9,10,15,20,30,50,100});
string allowednumberstring=string.Join(“,”,allowedNumbers.Select(n=>n+“€”);
控制台.WriteLine(“选择要下注的钱:”+allowedNumbersString);
而(!int.TryParse(Console.ReadLine(),out int y)| |!allowedNumbers.Contains(y))
{
Console.WriteLine(“选择正确的赌注:“+allowedNumbersString”);
}

我很不清楚你的问题是什么。你说的“更进一步”是什么意思?代码不是你想要的吗?你的用户输入是什么?预期的结果是什么?考虑使用一个集合,如<代码> HasStuts<代码>,条件应该是<代码>!内锥虫(…)| | y!=1&&y!=2 && ... && y!=100
或同等产品。现在你有一个无限循环。但是,请考虑使用<代码> HASSESET 或<代码>开关< /代码>语句
Console.OutputEncoding = System.Text.Encoding.UTF8;
HashSet<int> allowedNumbers = new HashSet<int>(new int[] { 1, 2, 3, 4, 5, 6, 8, 9, 10, 15, 20, 30, 50, 100 });
string allowedNumbersString = string.Join(", ", allowedNumbers.Select(n => n + "€"));

Console.WriteLine("Choose money to bet: " + allowedNumbersString);
while (!int.TryParse(Console.ReadLine(), out int y) || !allowedNumbers.Contains(y))
{
    Console.WriteLine("Choose correct money to bet: " + allowedNumbersString);
}