簡體   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