using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LIANXI
{
class Program
{
static void Main(string[] args)
{
string str=Console.ReadLine();
bool isAllUpperChar=true;
for (int i = 0; i < 5; i++)
{
if(str[i] >= 'A' && str[i] <= 'Z')
{
}
else
{
isAllUpperChar = false;
break;
}
}
if (isAllUpperChar==false)
{
Console.WriteLine("你输入的字母,不全是大写字母");
}
Console.ReadKey();
}
}
}
按理不会有错,但是不知道具体什么问题,各位该怎么改正
越界问题,你调试一下,应该很好找,你怎么知道一定小于5呢,看你输入的具体内容
for (int i = 0; i < str.Length; i++)
{
...
}
你这样写,如果str数组只有4个元素呢,那执行到str[4]的时候是不是就会越界了。
所以应该像楼上的那种写法,判断条件应该为i < str.length;
你的tr直接就是"",长度当然不够,你看看下面这个
static void Main(string[] args)
{
string str = Console.ReadLine();
bool isAllUpperChar = true;
for (int i = 0; i < str.Length; i++)
{
if (Convert.ToChar(str.Substring(i, 1)) >= 'A' && Convert.ToChar(str.Substring(i, 1)) <= 'Z')
{
}
else
{
isAllUpperChar = false;
break;
}
}
if (isAllUpperChar == false)
{
Console.WriteLine("你输入的字母,不全是大写字母");
}
Console.ReadKey();
}