好吧所以我正在制作一个程序来验证一个4位数的代码。
计算机生成4位数代码 用户输入4位数代码。他们猜测。 计算机告诉他们有多少位数 在正确的地方猜对了,有多少位数 被猜对了但是在错误的地方。 用户可以获得12次猜测 - 猜猜正确的代码。要么 输了 - 用完猜测。
所以基本上,我的程序似乎并没有真正验证代码是否正确但我不知道为什么不能,因为我有if和for循环进行验证,请看看。
class Program
{
public static Random random = new Random();
static void Main(string[] args)
{
int DigitOne = random.Next(0, 10);
int DigitTwo = random.Next(0, 10);
int DigitThree = random.Next(0, 10);
int DigitFour = random.Next(0, 10);
byte[] code = new byte[4];
code[0] = Convert.ToByte(DigitOne);
code[1] = Convert.ToByte(DigitTwo);
code[2] = Convert.ToByte(DigitThree);
code[3] = Convert.ToByte(DigitFour);
bool CodeCorrect = false;
Console.WriteLine(code[0] +""+ code[1] +""+ code[2]+""+code [3] );
Console.WriteLine("You have 12 guesses before you will be permenantly locked out.\n");
int AmountOfGuesses = 0;
while (AmountOfGuesses < 12 && !CodeCorrect)
{
Console.WriteLine("Enter 4 digit code to unlock the safe: ");
int[] UserCode = new int[4];
for (int i = 0; i < 4; i++)
{
UserCode[i] = Convert.ToInt32(Console.Read()) - 48;
}
if (UserCode.Length != 4)
{
Console.WriteLine("Error. Try Again.\n");
}
else
{
int UserDigitOne = UserCode[0];
int UserDigitTwo = UserCode[1];
int UserDigitThree = UserCode[2];
int UserDigitFour = UserCode[3];
for (int i = 0; i < 4; i++)
{
if (UserCode[i] == code[i])
{
Console.WriteLine("The digit at position " + (i + 1) + " is correct.");
}
}
if (UserCode[0] == code[0] && UserCode[1] == code[1] && UserCode[2] == code[2] && UserCode[3] == code[3])
{
CodeCorrect = true;
Console.WriteLine("Code Correct. Safe unlocked.");
}
}
AmountOfGuesses++;
}
if (AmountOfGuesses > 12)
{
Console.WriteLine("Code Incorrect. Safe Locked permenantly.");
}
Console.ReadLine();
}
答案 0 :(得分:1)
您正在将数字与数字的字符表示进行比较。 code[]
的每个值代表实际数字。然后,将这些值与UserCode
中的值进行比较,这是一个字符串,这意味着每个索引都有一个字符。从来没有((byte)'4') == ((byte)4)
(使用4作为示例,但适用于任何数字)。
解决此问题的一种方法是使用byte.Parse
方法将每个用户输入字符解析为一个字节。
为了有趣的学习目的,请查看以下代码的输出:
for (char i = '0'; i <= '9'; i++)
{
Console.WriteLine("char: " + i + "; value: " + ((byte)i));
}
输出实际上是:
char: 0; value: 48
char: 1; value: 49
char: 2; value: 50
char: 3; value: 51
char: 4; value: 52
char: 5; value: 53
char: 6; value: 54
char: 7; value: 55
char: 8; value: 56
char: 9; value: 57
这是由于字符串编码。
我还建议您使用代码,然后将其提交给Code Review网站上的优秀人员,以查看可能使用工作的代码的其他方面。
答案 1 :(得分:1)
如果在生成数字1246之后单步执行代码,然后从命令行输入相同的数字,将其转换为char数组,然后将每个char转换为一个字节,您将获得以下四个字节:
49 50 52 54
这些对应于每个字符的ASCII表示,而不是实际的数字。
尝试这样的事情:
int[] input = new int[4];
for(int i = 0; i < 4; i++ )
{
input[i] = Convert.ToInt32(Console.Read()) - 48;
}
-48应该将您的ASCII代码转换为提供的实际数字表示。 Console.Read()读取单个字符而不是整行。
此外,您不必说:
CodeCorrect == false
这更简单地表示为:
!CodeCorrect
同样,如果将其设置为true,则只会是:
CodeCorrect
我还建议使用for循环在数组中设置多个元素,而不是手动写出每行代码。对于小型阵列来说这不是什么大问题,但这是一个很好的做法。
更新:这是完整程序的修订版本:
class Program
{
public static Random random = new Random();
static void Main(string[] args)
{
int[] randCombination = new int[4];
for (int i = 0; i < 4; i++)
{
randCombination[i] = random.Next(0, 10);
Console.Write(randCombination[i].ToString());
}
bool CodeCorrect = false;
Console.WriteLine("\nYou have 12 guesses before you will be permenantly locked out.\n");
int AmountOfGuesses = 0;
while(AmountOfGuesses < 12 && !CodeCorrect)
{
Console.WriteLine("Enter 4 digit code to unlock the safe: ");
int[] UserCode = new int[4];
string input = Console.ReadLine();
int n;
bool isNumeric = int.TryParse(input, out n);
int correctCount = 0;
if(input.Length != 4 || !isNumeric)
{
Console.WriteLine("Error. Input code was not a 4 digit number.\n");
}
else
{
for(int i = 0; i < 4; i++)
{
UserCode[i] = Convert.ToInt32(input[i]) - 48;
if(UserCode[i] == randCombination[i])
{
Console.WriteLine("The digit at position " + (i + 1) + " is correct.");
correctCount++;
}
}
if(correctCount == 4)
{
CodeCorrect = true;
Console.WriteLine("Code Correct. Safe unlocked.");
}
}
AmountOfGuesses++;
}
if(AmountOfGuesses >= 12)
{
Console.WriteLine("Code Incorrect. Safe Locked permenantly.");
}
Console.ReadLine();
}
}
有些事情发生了变化:
Console.ReadLine()
的方式。其原因是检查用户是否输入了四位整数。 int.TryParse
语句确保输入为int,并且Length属性检查长度。 编辑#2:有关int.TryParse的更多信息,请参阅以下内容: