How to read three values in the same line? C#

Asked

Viewed 2,304 times

-1

2 answers

2


You can use String.Split to separate into spaces and then convert to decimal array.

using System;
using System.Linq;
namespace Exemplo
{
public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Múltiplos valores em uma linha");
            string inp = Console.ReadLine();
            decimal [] valores = inp.Split(' ').Select(x=> decimal.Parse(x)).ToArray();//Ou foreach se preferir.
            Console.WriteLine(string.Join(", ",valores));

        }
    }
}

0

That’s a pretty easy way to do it:

using System;

namespace uri
{
    class Program
    {
        static void Main(string[] args)
        {  
            string[] texto = Console.ReadLine().Split(" ");
            int n1 = int.Parse(texto[0]), n2 = int.Parse(texto[1]), n3 = int.Parse(texto[2]);
            Console.WriteLine("n1 = "+n1+", n2 = "+n2+", n3 = "+n3);        
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.