【问题标题】:Taking user input as name of new class instance将用户输入作为新类实例的名称
【发布时间】:2020-01-24 02:53:10
【问题描述】:

我正在尝试在 C# 中创建一个名为 customer 的类,其中包含 3 个变量:名称、初始存款和每月存款金额。

这是一个控制台程序,它接受用户输入这三个变量并不断要求更多用户,直到用户不输入任何内容并按下回车键。

然而,这条线 customer userInputName = new customer(userInputName, userInputInitial, userInputMonthly); 给我错误。第一个 userInputName 带有下划线,表示 “不能在此范围内删除名为 'userInputName' 的本地或参数,因为该名称在封闭的本地范围中用于定义本地或参数”。第二个 'userInputName' 表示 “参数 1:无法从 'lab4.Program.customer' 转换为 'string'”

我可以解决此问题的唯一方法是将第一个 'userInputName' 更改为类似 customer1 的名称,但如果我这样做,如果用户继续输入姓名,我将无法继续结交新客户。

理想情况下,我希望能够输入诸如 customer.Bob.initialDeposit 之类的内容,并让程序能够告诉我 Bob 的初始存款是什么,等等。

我怎样才能做到这一点,或者我做错了什么?

using System;

namespace lab4
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("How many months will the customer keep the money in the account?");
            string monthsString = Console.ReadLine();
            int months = Int32.Parse(monthsString);

            bool run = true;
            while (run)
            {
                Console.WriteLine("Enter new customer name: ");

                string userInputName = Console.ReadLine();
                if (userInputName == "")
                {
                    run = false;
                }
                else
                {
                    Console.WriteLine("Enter initial deposit amount: ");
                    string stringInitDeposit = Console.ReadLine();
                    int userInputInitial = Int32.Parse(stringInitDeposit);

                    Console.WriteLine("Enter montly deposit amount: ");
                    string stringMonthDeposit = Console.ReadLine();
                    int userInputMonthly = Int32.Parse(stringMonthDeposit);

                    customer userInputName = new customer(userInputName, userInputInitial, userInputMonthly);
                }

            }
        }


            public class customer
            {
                public string name;
                public int initialDeposit;
                public int monthlyDeposit;

                public customer(string name, int initialDeposit, int monthlyDeposit)
                {
                    this.name = name;
                    this.initialDeposit = initialDeposit;
                    this.monthlyDeposit = monthlyDeposit;
                }
            }
    }
}

【问题讨论】:

  • “我可以解决这个问题的唯一方法是将第一个 'userInputName' 更改为 customer1 之类的东西” -- 不,一点也不。您还可以通过将第二个更改为customer11userInputName 以外的任何其他内容来解决此问题。但是您的问题不可能提供一个好的答案,因为解决它的第三种方法是完全省略第二个 userInputName 变量。无论如何,您的代码永远不会对该变量执行任何操作,因此不清楚您实际期望在这里发生什么。请改进您的问题,以便清楚您真正需要什么。
  • 我用代码写了我想要做的事情。 理想情况下,我希望能够输入诸如 customer.Bob.initialDeposit 之类的内容,并让程序能够告诉我 Bob 的初始存款是什么,等等。 我想让每个新客户的名字成为变量的名称,但从下面 ps2goat 的响应中,我明白我哪里出错了。

标签: c# class oop object


【解决方案1】:

你有

string userInputName = Console.ReadLine();

customer userInputName = new customer(userInputName, userInputInitial, userInputMonthly);

您正试图重复使用相同的变量名。为客户选择一个新变量(因为无论如何,名称对此没有意义),并更新引用以使用该新变量名称。

如果您想结交多个客户,请将新客户添加到数组中。该变量仍然可以在 while 循环中重复使用。

例子:

// add this line outside (above) the while loop: (you will need to import the proper namespace for this: `using System.Collections.Generic;`)
List<Customer> newCustomers = new List<Customer>();


// I renamed this variable. add the line below to put the new customer into the list
customer newCustomer = new customer(userInputName, userInputInitial, userInputMonthly);
newCustomers.Add(newCustomer);

// now you have a list of new customers you can reference outside the while loop.

【讨论】:

  • 感谢您的回答,它完全按照我的预期工作。如果我理解正确,我会将每个新客户及其名称、初始存款和每月存款的所有属性添加到列表中,对吗?
  • 没错。下一次循环时,newCustomer 变量被重新创建,因此不会覆盖之前的客户数据。
【解决方案2】:

要获得 Bob 的押金,您可以执行以下操作:

using System;
using System.Collections.Generic;
using System.Linq;

(...)

var customers = new List<Customer>();

//Your logic to create customers here
var c1 = new Customer("Bob", 100, 10);
customers.Add(c1);
var c2 = new Customer("Bob", 200, 20);
customers.Add(c2);
var c3 = new Customer("Alice", 100, 10);
customers.Add(c3);


//Find all Bobs
var bobs = customers.Where(c => c.Name == "Bob");

foreach (var bob in bobs )
{
    Console.WriteLine($"Bob's initial deposit is {bob.InitialDeposit}");
}

请注意,类的名称通常大写,例如Customer.

在类中保持字段私有并使用属性进行公共访问也很常见。

这是 Customer 类的一个变体:

public class Customer
{
    public Customer(string name, int initialDeposit, int monthlyDeposit)
    {
        Name = name;
        InitialDeposit = initialDeposit;
        MonthlyDeposit = monthlyDeposit;
    }

    public string Name { get; }
    public int InitialDeposit { get; }
    public int MonthlyDeposit { get; }
}

【讨论】:

  • 是的,我在阅读完整问题之前就开始回答了。现已更新。
猜你喜欢
  • 2014-05-07
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
  • 2022-07-20
  • 2020-08-17
  • 2018-07-04
  • 1970-01-01
相关资源
最近更新 更多