C# 为存放收音机的仓库编写一个类

C# 为存放收音机的仓库编写一个类,c#,C#,“为存放收音机的仓库编写一个类, 电视和计算机。提供用于启动 没有项目的仓库。包括实例变量以存储 仓库中每个项目的数量。包括添加到 每个项目的库存,以及显示仓库内容的方法。 在主方法中进行测试,创建两个仓库。向每个仓库添加项目,然后 显示每个仓库的最终内容。” using System; public class warehouse { int radios, televisions, computers; public warehouse() { ra

“为存放收音机的仓库编写一个类, 电视和计算机。提供用于启动 没有项目的仓库。包括实例变量以存储 仓库中每个项目的数量。包括添加到 每个项目的库存,以及显示仓库内容的方法。 在主方法中进行测试,创建两个仓库。向每个仓库添加项目,然后 显示每个仓库的最终内容。”

using System;
public class warehouse
{
    int radios, televisions, computers;

    public warehouse()
    {
        radios = televisions = computers = 0;
    }

    public void addRadioStock()
    {
        Console.WriteLine("Please enter the number of radios in stock.");
        string radiosInput = Console.ReadLine();
        Convert.ToInt16(radiosInput);
    }

    public void addTelevisionsStock()
    {
        Console.WriteLine("Please enter the number of televisions in stock.");
        string televisionsInput = Console.ReadLine();
        Convert.ToInt16(televisionsInput);
    }

    public void addComputersStock()
    {
        Console.WriteLine("Please enter the number of computers in stock.");
        string computersInput = Console.ReadLine();
        Convert.ToInt16(computersInput);
    }

    public void Display()
    {

        Console.WriteLine(" There are " + radios + " radios in stock.");
        Console.WriteLine(" There are " + televisions + " televisions in stock.");
        Console.WriteLine(" There are " + computers + " computers in stock.");
        Console.WriteLine();
    }

    public static void Main()
    {
        warehouse warehouse1 = new warehouse ();
        warehouse warehouse2 = new warehouse ();
        warehouse1.addRadioStock ();
        warehouse1.addTelevisionsStock ();
        warehouse1.addComputersStock ();
        warehouse2.addRadioStock ();
        warehouse2.addTelevisionsStock ();
        warehouse2.addComputersStock ();
        Console.WriteLine("Warehouse 1:");
        warehouse1.Display();
        Console.WriteLine("Warehouse 2:");
        warehouse2.Display();
    }
}

在转换为
int
后,无法获取您函数的输出我做错了什么

将值分配给类的成员

例如,更改如下:

public void addRadioStock()
{
    Console.WriteLine("Please enter the number of radios in stock.");
    string radiosInput = Console.ReadLine();
    Convert.ToInt16(radiosInput);
}
为此:

public void addRadioStock()
{
    Console.WriteLine("Please enter the number of radios in stock.");
    string radiosInput = Console.ReadLine();
    radios = Convert.ToInt16(radiosInput);
}
我还建议:

  • 查看
    int.TryParse
  • 办理登机手续。以及更全面的外观
  • 你的问题是:

     Convert.ToInt16(computersInput);
    

    Convert.ToInt16解析输入并返回数字。在每种情况下,您都不会将返回的号码分配给任何内容

    你“不能”是什么意思,会发生什么?请给出你的问题标题,不要听起来像是懒惰的请求,并阅读@Mohammed Kalimulla-这有助于你解决问题吗?