C#传递生成递归
我有一个使用ascii表的密码生成器。我正在尝试学习如何使用递归。我知道有很多简单的方法可以生成一个生成器,但我想学习更多关于递归的知识。我希望它选择字母(65-90),但也选择数字(000-009)C#传递生成递归,c#,recursion,C#,Recursion,我有一个使用ascii表的密码生成器。我正在尝试学习如何使用递归。我知道有很多简单的方法可以生成一个生成器,但我想学习更多关于递归的知识。我希望它选择字母(65-90),但也选择数字(000-009) 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 类密码生成器 { 静态void Main() { 布尔旁路=假; string errorMessage=“这是一个无效的数字,请重试”; string howManyCharac
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
类密码生成器
{
静态void Main()
{
布尔旁路=假;
string errorMessage=“这是一个无效的数字,请重试”;
string howManyCharacters=“您希望密码包含多少个字符?(按“0”停止)”;
string pressKeyMessage=“按任意键继续”;
整数长度=0;
做
{
var passwordBuilder=new StringBuilder();
Console.WriteLine(有多少个字符);
var inputIsNumber=int.TryParse(Console.ReadLine(),out-length);
如果(!inputIsNumber)
{
控制台写入线(错误消息);
旁路=假;
}
else if(inputIsNumber&&length!=0)
{
for(int i=0;i
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
类密码生成器
{
静态void Main()
{
布尔旁路=假;
整数长度=0;
做
{
var passwordBuilder=new StringBuilder();
Console.WriteLine(“您希望密码包含多少个字符?(按“-1”停止)”;
///尝试方法在里面,取代了旧的尝试和捕捉
var Input_是_Int=Int.TryParse(Console.ReadLine(),out length);
如果(!Input_是_Int | | length==0)
{
Console.WriteLine(“该号码无效,请重试”);
Console.WriteLine();
旁路=假;
}
else if(输入为Int&&length!=-1)
{
for(int i=0;i(char)i.ToArray();
静态随机_r=新随机();
静态字符GetRandomChar()
{
var index=\u r.Next(0,\u lookupTable.Length);
返回_lookupTable[索引];
}
}
你确定你指的是递归而不仅仅是重复迭代吗?你确定你明白什么是递归吗?我可能在某个地方漏掉了它,但在你的代码中找不到递归引用。“要理解递归,首先你必须理解递归。“提示:递归通常需要一个不是主函数的函数,除非您正在执行程序递归,这是不推荐的。请将此作为一个问题进行改革-不清楚您实际遇到了什么问题。这正是我试图弄明白的,但我没有话要说,谢谢。很高兴我能帮上忙。现在你可以通过接受我的答案来帮助我唯一的问题是不需要转储
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class PasswordGenerator
{
static void Main()
{
bool bypass = false;
string errorMessage = "That is an Invalid Number, Please try again";
string howManyCharacters = "How many Characters would you like the Password to be? (Press '0' to Stop)";
string pressKeyMessage = "Press any key to continue";
int length = 0;
do
{
var passwordBuilder = new StringBuilder();
Console.WriteLine(howManyCharacters);
var inputIsNumber = int.TryParse(Console.ReadLine(), out length);
if (!inputIsNumber)
{
Console.WriteLine(errorMessage);
bypass = false;
}
else if (inputIsNumber && length != 0)
{
for (int i = 0; i < length; i++)
passwordBuilder.Append(GetRandomChar());
Console.WriteLine();
Console.WriteLine("Password: " + passwordBuilder.ToString());
Console.WriteLine();
}
else
{
Console.WriteLine();
Console.WriteLine(pressKeyMessage);
Console.ReadLine();
bypass = true;
}
}
while (!bypass);
}
static Random _r = new Random();
static char GetRandomChar()
{
return (char)_r.Next(65, 90); // A through Z
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class PasswordGenerator
{
static void Main()
{
bool bypass = false;
int length = 0;
do
{
var passwordBuilder = new StringBuilder();
Console.WriteLine("How many Characters would you like the Password to be? (Press '-1' to Stop)");
/// try method is inside, replaced old try and catch
var Input_is_Int = int.TryParse(Console.ReadLine(), out length);
if (!Input_is_Int || length == 0)
{
Console.WriteLine("That is an Invalid Number, Please try again");
Console.WriteLine();
bypass = false;
}
else if (Input_is_Int && length != -1)
{
for (int i = 0; i < length; i++)
passwordBuilder.Append(GetRandomChar());
Console.WriteLine("Password: " + passwordBuilder.ToString());
Console.WriteLine();
}
else
{
Console.WriteLine();
Console.WriteLine("Press any key to continue");
Console.ReadLine();
bypass = true;
}
}
while (!bypass);
}
/// Union of ABCDEFGHIJKLMNOPQRSTUVWXYZ, 0123456789 and !"#$%&'()*+,-./
static char[] _lookupTable = Enumerable.Range(65, 26).Union(Enumerable.Range(48, 10).Union(Enumerable.Range(33, 14))).Select(i => (char)i).ToArray();
static Random _r = new Random();
static char GetRandomChar()
{
var index = _r.Next(0, _lookupTable.Length);
return _lookupTable[index];
}
}