繁体   English   中英

将文本数据保存到数组

[英]Save text data to array

我对使用C#非常陌生。 我正在尝试保存输入到控制台中的数据(例如人名之类的文本),然后“读取”到数组。

我要将数据保存到的数组的名称是: name2convert
收集数据(要转换的名称)的变量为: nameEntered

很感谢任何形式的帮助。 我已经为此工作了几个小时,并进行了几次搜索,但是由于我目前对C#的了解有限,我找不到任何可以理解的答案。 我只尝试学习了几周-我非常绿色。 任何帮助表示赞赏。

注意:字符串名称是我的测试数组,因此我可以看到我知道如何从数组读回数据。

我想将数据保存到names2Convert数组。

这是我的代码:

using System;

namespace a061___String_Manipulations___PigLatin
{
///loop - ask for number of names equal to number asked
///  read line, save to array, iterate one up until num equals value asked for

class Program
{
    //Arrays

    String[] names = { "test01", "test02", "test03", "test04", "test05" }; //Test loop

    String[] name2convert = new String[1];

    //Variables & Ints?
    string title = ">>>-- Welcome to the Highly Enlightening World of Igp-ay Atinl-ay --<<< \n";
    string totalIs = "You said you want to convert a total of";
    string listCommands = "Is that correct? If so type (Y)es, (R)enter or (Q)uit";// general commands used
    string addSuffix ="-ah!"; // Add to end of each name
    string nameEntered = "";//name to be converted

    int namesTotal = 0;//

    //Main Method
    public void Play()
    {
        Console.WriteLine(title); //announce program

        askTotal(); //ask number of names

        while (true)
        {
            Console.WriteLine(listCommands);//lists options
            String command = Console.ReadLine().ToLower();//reads user command

            if (command == "y") // if askTotal true save to array? how?
            { 
                askName();//collects name entered
                confirmName();//allows user to confirm spelling, etc.    

                //y save the array   nameEntered   name2convert
                //name2convert.Add(nameEntered);
                name2convert[0] = nameEntered;

                //confirm name 
                for (int i = 0; i < name2convert.Length; i++)
                {
                    Console.WriteLine("Name Aquired: " + name2convert[i]);
                }
            }
            else if (command == "r")
            {
                askName();//asks name
            } 
            else if (command == "q")
            {
                Console.WriteLine("Cheers!"); break; //end
            }
            else
            {
                Console.WriteLine("Sorry. Invalid Request");//try again
            }

            PrintList();//test array 
        }
    }

    //Helper Methods
    public void PrintList()//iterates through, prints names stored in array
    {
        Console.WriteLine("Print List");
        for (int i = 0; i < names.Length; i++)
        {
            Console.WriteLine((i + 1) + ". " + names[i] + addSuffix);
        }
    }

    //iterates through, prints names stored in array
    public void askName()
    {
        Console.WriteLine("Enter Name: ");//Confirming
        String nameEntered = Console.ReadLine().ToLower();// Capture name

        Console.WriteLine("Name Captured: " + nameEntered);//confirming name caught
    }

    //iterates through, prints names stored in array
    public void confirmName()
    {
        Console.WriteLine(listCommands);//Confirming
        String command = Console.ReadLine().ToLower();
    }

    //how many names to convert
    public void askTotal() 
    {
        Console.WriteLine("How many names would you like to convert?");//Ask for content
        namesTotal = int.Parse(Console.ReadLine());

        Console.WriteLine(totalIs + " " + namesTotal);//Confirming
    }

    //Call Application
    static void Main(string[] args)
    {
        Program StringManipulations = new Program();
        StringManipulations.Play(); //Call forth the Pig Latin...

        Console.Read();//
    }
}

}

更改此:

//y save the array   nameEntered   name2convert
name2convert.Add(nameEntered);

对此:

name2convert[0] = nameEntered;

编辑:

askName()函数中更改:

String nameEntered = Console.ReadLine().ToLower();// Capture name 

至:

nameEntered = Console.ReadLine().ToLower();// Capture name

您已经具有声明为类属性的string类型的nameEntered

为什么要先使用string然后使用String 一样,因为stringString别名(实际上是C#中的System.String )-但要保持一致!

因为您已经为该数组分配了内存(它是固定大小-在您的情况下是一个)。 因此,要访问数组中的第一个(也是唯一的)单元,应使用name2convert[0] -0是任何数组的第一个索引,通常是C#(以及许多其他编程语言)中任何其他结构/容器的第一个索引。

另一种方法(如您在示例中所尝试的)是使用List<String> 有关数组和列表的更多信息,请参见此处:

数组教程

清单教程和示例

如果要保存用户输入的每个单词,请使用字符串列表,例如

 List<String> name2convert;

然后

name2convert.Add(nameEntered);

浏览清单

foreach (String word in name2convert) 
{
    Console.WriteLine(word);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM