如何使用c#在控制台上显示列表

时间:2018-01-10 02:19:58

标签: c# visual-studio-2015

我正在使用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();
    }
}

}

3 个答案:

答案 0 :(得分:1)

如果要显示某个集合,则需要使用 foreach for loop

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

答案 1 :(得分:0)

我建议你覆盖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();
    }
}

答案 2 :(得分:0)

至于从用户那里获得每月工资总额,我相信你可以使用与获取名字相同的方法。

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#的新手。