如果我想知道正确数字的数量,尽管我错位了,我想知道如何编码。例如,正确的是“ 7896”,但是我输入了“ 8749”,因此我希望它表明它具有正确的“ 3”(789)。
void Run()
{
// initialize the number of attempts
int numberOfAttempts = 1000;
Console.WriteLine("\nWelcome to Random Number Guessing Game.");
Console.WriteLine("\n\nGuess the 4 digit random number XXXX.");
Console.WriteLine("\nFor each digit, the number is chosen from 1 to 9 \nNumbers can repeat.");
// Call the method to Generate the Random Number
string randomNumber = GenerateRandomNumber();
for (int i = 1; i <= numberOfAttempts; i++)
{
// Call the method to get the user input
string userInput = GetUserInput(i);
// Get the result - Collection containing the result of all 4 digits
List<Result> result = GetResult(randomNumber, userInput);
// Guess the count of number of digits that are correct
int flagCount = result.Where(f => f.Flag == true).Count();
// Get the place(s)/index of digits that are correct
string digitsCorrect = string.Join(",", result.Where(f => f.Flag == true)
.Select(c => (++c.Index).ToString()));
// check the flag count and display appropriate message
if (flagCount == 4)
{
Console.WriteLine("Random Number:{0} , Your Input:{1}", randomNumber, userInput);
Console.WriteLine("You guess is correct! Game Won..hurray...:)");
break;
}
else
{
digitsCorrect = flagCount == 0 ? "none" : digitsCorrect;
Console.WriteLine(string.Format("Digit(s) in place {0} correct", digitsCorrect));
Console.WriteLine(flagCount);
}
}
Console.ReadLine();
}
我已经做了一些方法,可以玩了
参考方案
像这样吗
public static string Info(string guess, string actual)
{
int correctNumbers = 0;
string correctChars = "";
List<char> charlists = actual.ToList();
foreach (var char_ in guess)
{
if (charlists.Contains(char_))
{
correctNumbers++;
correctChars += char_.ToString();
charlists.Remove(char_);
}
}
return $"{correctNumbers}({correctChars})";
}
.NET C#Webbrowser填充输入,不带ID或类名 - javascript我需要在网络浏览器中填写一个输入,但这不起作用。我认为必须使用name属性来完成,但是怎么做呢?foreach (HtmlElement login in webBrowser1.Document.GetElementsByTagName("input")) { if (login.GetAttribute("name"…
ddl在服务器中未更新-asp.net - javascript我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……
asp.net mvc中的对象数组数据始终为null - javascript我需要通过json将对象数组发送到asp.net mvc 2,但是我在mvc控制器中没有得到null对象是这样的entries[1].date = "12/22/2014" entries[1].Ref = "0002" entries[1].Credit = "100" entries[2].da…
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
改造正在返回一个空的响应主体 - java我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…