【问题标题】:how can I display a list on console using c#如何使用 c# 在控制台上显示列表
【发布时间】:2018-01-10 02:19:57
【问题描述】:

我正在使用 Visual Studio 2015,我正在练习 C#。 用户输入信息后如何在控制台显示列表? 我可能做错了,我还在学习 C#。一旦我输入了所有信息,我如何让控制台显示收集的信息?我有一个固定的总收入数字,我不知道如何更改它,以便用户可以输入任何数字并计算它,(仍在努力)欢迎任何建议、链接、视频。我在看一些复数教程,但似乎没有展示如何做到这一点。

//My first program 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PracticeProgram
{
   public class Program
   {
    public static void Main(string[] args)
    {
        List<Person> myContacts = new List<Person>();

        //You could loop to collect developers data
        for (int i = 0; i < 4; i++)
        {
            Console.WriteLine("Welcome developer, enter your name to continue");
            string name = Console.ReadLine();
            Console.WriteLine("Hello, " + name);

            Console.WriteLine("enter your address");
            string address = Console.ReadLine();
            Console.WriteLine(address);

            Console.WriteLine("enter your monthly income");
            double GrossMonthlyPay = 10000;
            Console.WriteLine(GrossMonthlyPay);

            Console.WriteLine("your tax deduction set at 7% is ");
            double taxes = (0.07);
            Console.WriteLine(taxes = GrossMonthlyPay * taxes);

            Console.Write("\nPress any key to continue...");
            Console.ReadLine();

            myContacts.Add(new Person(name, address, GrossMonthlyPay, taxes));
        }

    }
}

internal class Person
{
    private string address;
    private double grossMonthlyPay;
    private string name;
    private double taxes;

    public Person(string name, string address, double grossMonthlyPay, double taxes)
    {
        this.name = name;
        this.address = address;
        this.grossMonthlyPay = grossMonthlyPay;
        this.taxes = taxes;
    }

    internal static Person ReadFromCSV(string line)
    {
        throw new NotImplementedException();
    }

    internal void SerializeToCSV(string v)
    {
        throw new NotImplementedException();
    }
}

}

【问题讨论】:

    标签: c# visual-studio-2015


    【解决方案1】:

    当你想显示一个集合时,你需要用 foreachfor loop

    循环它们
    foreach (Person element in myContacts)
    {
        System.Console.WriteLine(element.name);
        ....
        etc
    }
    

    【讨论】:

    • 那么这个 foreach 应该去哪里呢?这会像程序末尾的所有数据列表一样打印出来吗?我知道使用 Java,您可以像表格格式一样打印出数据。这可以用 C# 完成吗?
    【解决方案2】:

    我建议您覆盖 Person 的 ToString() 方法。这样,无论您想在哪里显示 Person 的字符串表示形式,它都会显示相同的内容:

    internal class Person
    {
        private string address;
        private double grossMonthlyPay;
        private string name;
        private double taxes;
    
        public Person(string name, string address, double grossMonthlyPay, double taxes)
        {
            this.name = name;
            this.address = address;
            this.grossMonthlyPay = grossMonthlyPay;
            this.taxes = taxes;
        }
    
        public override string ToString()
        {
            return "Name=" + this.name + ",Address =" + this.address + " Monthly Pay=" + this.grossMonthlyPay + " Taxes=" + this.taxes.ToString();
        }
    
        internal static Person ReadFromCSV(string line)
        {
            throw new NotImplementedException();
        }
    
        internal void SerializeToCSV(string v)
        {
            throw new NotImplementedException();
        }
    }
    

    【讨论】:

    • 感谢@Ctznkane525 这更有意义
    【解决方案3】:

    至于从用户那里获取每月总收入,我相信您可以使用与获取名称相同的方法。

    string GrossMonthlyPayInput = Console.ReadLine();
    double GrossMonthlyPay = 0;
    

    并且很可能您想确保只使用数字,以便您可以使用解析,点击此链接:Float/Double type input validation in C# 或从此处https://social.msdn.microsoft.com/Forums/windows/en-US/84990ad2-5046-472b-b103-f862bfcd5dbc/how-to-check-string-is-number-or-not-in-c?forum=winforms

    比如:

    if (double.TryParse(GrossMonthlyPayInput, out j))
        GrossMonthlyPay = double.Parse(GrossMonthlyPayInput);
    else
        Console.WriteLine("Please Input numbers only");
    

    我也没有尝试过这段代码,如果我误解了你的问题,我可能无法很好地回答你的问题,所以我先道歉,因为我也是 C# 新手。

    【讨论】:

      猜你喜欢
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      相关资源
      最近更新 更多