您好我正在创建一个Magic Square游戏,我创建了一个2D数组来存储零并创建一个3 x 3的板,看起来像这样...
0 0 0
0 0 0
0 0 0
然后我创建了一个for循环,要求用户输入一个[0,0]到[2,2]的整数,它结束所以它看起来像这样,假设它被输入1-9 ..
1 2 3
4 5 6
7 8 9
我的问题是如何让用户只输入1-9的号码,只能使用一次号码?
我尝试过:
尝试使用bool阵列,但我不知道如何设置
答案 0 :(得分:1)
简单地使用以前选择的数字列表
var previousNumbers = new List<int>(); // Create list of previous numbers entered
int number;
Console.WriteLine($"Enter new number from 1 to 9 or 0 to exit.");
var valueEntered = Console.ReadKey().KeyChar;
// try converting key press to an int and check its not 0
while (int.TryParse(valueEntered.ToString(), out number) && number != 0)
{
if (previousNumbers.Contains(number))
{
Console.WriteLine(" has already been entered, try again.");
}
else
{
// add you number to the list
previousNumbers.Add(number);
Console.WriteLine(" is a valid number and hasn't been used.");
}
// get next value and do the hokey pokey
valueEntered = Console.ReadKey().KeyChar;
}
由于受欢迎的需求,这是另一种方式
var previousNumbers = new HashSet<int>(); // Create list of previous numbers entered
int number;
Console.WriteLine($"Enter new number from 1 to 9 or 0 to exit.");
// try converting it to in, and check its not 0
while (int.TryParse(Console.ReadLine(), out number) && number >= 1 && number <= 9)
{
if (!previousNumbers.Add(number))
{
Console.WriteLine($"{number} has already been entered, try again.");
}
else
{
Console.WriteLine($"{number} is a valid number and hasn't been used.");
}
}
答案 1 :(得分:0)
只需检查号码并列出清单。
ActiveChart.ApplyChartTemplate ( _
"C:\Users\XXXXX\AppData\Roaming\Microsoft\Templates\Charts\1.crtx")