我需要制作一个获取随机字符并检查其ASCII值(int)的程序。
例如:
Console.WriteLine("Please enter your first character");
char num1 = int.char(Console.ReadLine());
if((character) >= 65 && (character) <= 90 && (character) >= 97 && (character) <= 122)
{
letters += 1;
}
该程序会像检查数字一样检查值,例如,如果我输入字母 F ,它将不会在字母计数器中加1。
答案 0 :(得分:1)
尝试一下
Console.WriteLine("Please enter your first character");
char num1 = int.char(Console.ReadLine());
if(((character) >= 65 && (character) <= 90) || ((character) >= 97 && (character) <= 122))
{
letters += 1;
}
您在AND-OR逻辑中犯了一个小错误。
答案 1 :(得分:-2)
您可以尝试以下代码;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter how many characters your password will have");
int num1 = int.Parse(Console.ReadLine());
int letters = 0;
int numbers = 0;
for (int i = 0; i < num1; i++)
{
Console.WriteLine("Please enter the character");
char character = char.Parse(Console.ReadLine());
if (character >= 'A' && character <= 'Z' || character >= 'a' && character <= 'z')
{
letters += 1;
}
if(character >= '0' && (int)character <= '9')
{
numbers += 1;
}
if (character <= 'A' && character >= 'Z' || character <= 'a' && character >= 'z' || character <= '0' && (int)character >= '9')
{
Console.WriteLine("you have entered invalid character");
}
}
if(letters >= numbers)
{
Console.WriteLine("The password has " + num1 + " characters and its legal");
}
else
{
Console.WriteLine("The password has " + num1 + " characters and it is illegal");
}
Console.ReadLine();
}
}
}