C# 为什么我总是出错
对于这段代码,我一直得到一个错误。当我输入“A”时,它确实会显示“请输入生成的金额”,然后它会显示一个错误C# 为什么我总是出错,c#,loops,C#,Loops,对于这段代码,我一直得到一个错误。当我输入“A”时,它确实会显示“请输入生成的金额”,然后它会显示一个错误 static void Main(string[] args) { string SalesPerson; int Sales = 0; Console.WriteLine("Please enter the salesperons's initial"); SalesPerson = Console.ReadLine().ToUpper();
static void Main(string[] args)
{
string SalesPerson;
int Sales = 0;
Console.WriteLine("Please enter the salesperons's initial");
SalesPerson = Console.ReadLine().ToUpper();
while (SalesPerson !="Z")
{
if (SalesPerson=="A")
{
Console.WriteLine("Please enter amount of a sale Andrea Made");
Sales = Convert.ToInt32(SalesPerson);
}
}
}
您正在混合字符串和整数。Sales是一个int,SalesPerson是一个字符串,在您描述的例子中,是“a” 因此,当您尝试以下方法时:
Sales = Convert.ToInt32(SalesPerson);
…它失败,因为“A”(SalesPerson字符串的值)无法转换为整数。“巨大”的错误可能基本上告诉了您这一点。您可以尝试以下方法:
static void Main(string[] args)
{
string SalesPerson;
int Sales = 0;
//The loop to ask the user for a letter
do
{
Console.WriteLine("Please enter the salesperons's initial");
SalesPerson = Console.ReadLine().ToUpper();
//If the letter is not equal to "A", repeat the prompt
}while (SalesPerson != "A")
if (SalesPerson=="A")
{
Console.WriteLine("Please enter amount of a sale Andrea Made");
Sales = Convert.ToInt32(SalesPerson);
}
}
那是什么语言,C?请编辑您的问题并添加相关标签。我怀疑这个错误是关于循环的,所以这个标记也可能是错误的。您还存在一些缩进问题。此外,标题对识别您的问题没有帮助,您没有显示确切的错误消息,这也不是一个错误。请花点时间改进您的问题!这个错误有多大?它的症结是什么?请看标记的重复,这是一个性质完全相同的同样过于宽泛的问题。请注意,在您的代码中,您知道
SalesPerson
的值是“A”
。您希望Convert.ToInt32()
将其转换为什么整数值?是什么让你认为这是一个有效的转换?