如何在C#中进行tupple输入? [英] How to take tupple input in C#?

查看:104
本文介绍了如何在C#中进行tupple输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#.NET中使用元组输入?



这些是3元组下方



西班牙英格兰3 0

英格兰法国1 1

西班牙法国0 2



i想要n元组在运行时输入。

一个元组包含4个项目。

项目1 =字符串

项目2 =字符串

项目3 = int

项目4 = int



i想要获取运行时间元组输入 - (西班牙,英格兰, 3,0)



我的尝试:



how can i take tuple input in C#.NET?

these are 3 tuple below

Spain England 3 0
England France 1 1
Spain France 0 2

i want to take n tuples input at run time.
one tuple contains 4 items.
item 1 = string
item 2 = string
item 3= int
item 4= int

i want to take runtime time tuple input -("Spain", "England", 3, 0)

What I have tried:

Tuple<string,string,int,int> tuple = new Tuple<string, string, int, int>("Spain", "England", 3, 0);

          for (int i = 0; i <m; i++)
           {
              Console.ReadLine(tuple.Item1,tuple.Item2,tuple.Item3,tuple.Item4);
          }

推荐答案

请记住:[ ^ ]。
Quote:

Tuple类本身并不代表元组。相反,它是一个提供静态方法的类,用于创建.NET Framework支持的元组类型的实例。它提供了可以调用实例化元组对象的辅助方法,而无需显式指定每个元组组件的类型。

The Tuple class does not itself represent a tuple. Instead, it is a class that provides static methods for creating instances of the tuple types that are supported by the .NET Framework. It provides helper methods that you can call to instantiate tuple objects without having to explicitly specify the type of each tuple component.

可以使用'Tuple.Create方法(最多包含8个参数)来简化元组创建,以及保存你的手指一些输入:

The 'Tuple.Create method can be used (with up to eight parameters) to simplify Tuple creation, and save your fingers some typing:

var matchScoreTuple = Tuple.Create("Spain", "England", 3, 0);

如果您希望用户输入每组数据它将成为控制台中单行的元组:

If you want the user to enter each group of data that will become a Tuple on a single line in the Console:

private char[] splitChars = new[] {','};
int s1, s2;

string matchResult = Console.ReadLine();

string[] matchData = matchResult.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);

if (matchData.Length != 4)
{
    Console.WriteLine("string, string, int, int separated by commas required");
}
else
{
    if(
        Int32.TryParse(matchData[2], out s1)
        &&
        Int32.TryParse(matchData[3], out s2)
    )
    {
        var match = Tuple.Create(
            matchData[0],
            matchData[1],
            matchData[2],
            matchData[3]
        );

        // now do something with the Tuple
    }
    else
    {
        Console.WriteLine("error in integer entry values");
    }
}

注意:当然这是不完整的代码,你可能希望有一个循环,用户可以输入多个匹配结果,你想要存储你创建的有效元组......某处。

Note: of course this is incomplete code, you would want to probably have a loop in which the user could enter multiple match outcomes, and you'd want to store the valid Tuples you create ... somewhere.


试试这样



try like this

Tuple<string, string, int, int> tuple = new Tuple<string, string, int, int>(Console.ReadLine(), Console.ReadLine(), int.Parse( Console.ReadLine()),int.Parse( Console.ReadLine()));





tupple 项目是只读,它只能在创建元组对象时初始化,一旦创建了对象,项目就无法修改。



更好的方式:





tupple items are read only , It can only be initialised while creating the tuple object, once the object is created, the items cannot be modified.

better way:

string item1 = Console.ReadLine();
        string item2 = Console.ReadLine();
        int item3, item4;
        int.TryParse(Console.ReadLine(), out item3);
        int.TryParse(Console.ReadLine(), out item4); 
        Tuple<string, string, int, int> tuple = new Tuple<string, string, int, int>(item1, item2, item3, item4);





根据账单评论更新





updated based on bill's comment

    Console.WriteLine("Please enter item1: string ");
    string item1 = Console.ReadLine();
    Console.WriteLine("Please enter item2: string ");
    string item2 = Console.ReadLine();
    int item3, item4;
i3: Console.WriteLine("Please enter item3: integer ");
    if (!int.TryParse(Console.ReadLine(), out item3))
    {
        Console.WriteLine("InValid integer value ");
        goto i3;
    }
i4: Console.WriteLine("Please enter item4: integer ");
    if (!int.TryParse(Console.ReadLine(), out item4))
    {
        Console.WriteLine("InValid integer value ");
        goto i4;
    }

    Tuple<string, string, int, int> tuple = new Tuple<string, string, int, int>(item1, item2, item3, item4);
    Console.WriteLine("Your tuple is => {0},{1},{2},{3}",tuple.Item1,tuple.Item2,tuple.Item3,tuple.Item4);
    Console.ReadLine();


这篇关于如何在C#中进行tupple输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆