Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我总是出错_C#_Loops - Fatal编程技术网

C# 为什么我总是出错

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();

对于这段代码,我一直得到一个错误。当我输入“A”时,它确实会显示“请输入生成的金额”,然后它会显示一个错误

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()
将其转换为什么整数值?是什么让你认为这是一个有效的转换?