【问题标题】:Getting a value cannot be null error with my C# project使用我的 C# 项目获取值不能为空错误
【发布时间】:2021-03-28 15:48:23
【问题描述】:

System.ArgumentNullException: '值不能为空。 (参数's')'

我收到此错误^。

我想这样做,所以在我在产品代码上按 CTRL Z 后,它将停止循环并继续执行下一行代码。但是我输入了 ctrl z ,它进入了 Quantity 行并在那之后中断。如果有人知道如何解决此错误,我认为这是因为 Quantity 无法返回 Null 值,因为它不像产品那样设置,但我找不到解决方法。

public class Transaction
{
    public string Name { get; set; }
    public int productCode { get; set; }
    public int productQuantity { get; set; }

    //constructor

    public Transaction(string cName)//, int pCode, int pQuantity
    {
        Name = cName;
    }

    public void AssessProducts()
    {

        int codeInput;
        int qtyInput = 0;
        decimal transTotal = 0;
        double qty201 = 0;
        double qty556 = 0;
        double count201 = 0;
        double count556 = 0;
        double count558 = 0;
        double count909 = 0;
        double count910 = 0;

        Console.WriteLine("Product Code: ");
        string ctrlzInput = Console.ReadLine();
        Console.WriteLine("Quantity: ");
        qtyInput = int.Parse(Console.ReadLine());

        while (ctrlzInput != null)
        {
            codeInput = int.Parse(ctrlzInput);

            if (codeInput == 201)
            {
                count201 = count201 + 19.99;
            }
            else if (codeInput == 556)
            {
                count556 = count556 + 59.99;
            }
            else if (codeInput == 558)
            {
                count558 = count558 + 20.50;
            }
            else if (codeInput == 909)
            {
                count909 = count909 + 105.99;
            }
            else if (codeInput == 910)
            {
                count910 = count910 + 16.99;
            }
            else
            {
                Console.WriteLine("Error! Wrong product code.");
            }

            transTotal = (decimal)((double)transTotal + count201 + count556 + count558 + count909 + count910);
            qty201 = qty201 + qtyInput;
            qty556 = qty556 + qtyInput;


          Console.WriteLine("Product Code: ");
          ctrlzInput = Console.ReadLine();
          Console.WriteLine("Quantity: ");
          qtyInput = int.Parse(Console.ReadLine());

        }
                
       Console.WriteLine($"Customer Name: {Name} \n\n");
       Console.WriteLine("Items Ordered:\n\n");
       Console.WriteLine("Product #    Product Name              Price            Qty         Line Total\n");
        if (count201 >= 1)
        {
            Console.Write($"Product 210 - ACME Anvil               @ $19.99       *{qty201}      = {qty201 * 19.99} \n");
        }
        if (count556 >= 1)
            Console.Write($"Product 556 - ACME Dynamite            @ $59.99       *{qty556}      = {qty556 * 59.99} \n");
        }
              
    }

【问题讨论】:

  • 请不要将代码发布为图像,发布为文本
  • Int.Parse 仅适用于实数。使用 int.TryParse 进行仔细检查
  • 请检查 null 或空值是否通过。

标签: c#


【解决方案1】:

来自documentation

退货

字符串

输入流中的下一行字符,如果没有更多行可用,则为null

还有:

如果在方法从控制台读取输入时按下 Ctrl+Z 字符,则该方法返回null。这使用户能够在循环调用ReadLine 方法时阻止进一步的键盘输入。

【讨论】:

    猜你喜欢
    • 2013-01-26
    • 2015-11-07
    • 1970-01-01
    • 2017-11-08
    • 2014-01-30
    • 2017-12-16
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    相关资源
    最近更新 更多