reading two integers in one line using C#

2019-01-07 21:16发布

i know how to make a console read two integers but each integer by it self like this

int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());

if i entered two numbers, i.e (1 2), the value (1 2), cant be parse to integers what i want is if i entered 1 2 then it will take it as two integers

12条回答
We Are One
2楼-- · 2019-01-07 21:39
public static class ConsoleInput
{
    public static IEnumerable<int> ReadInts()
    {
        return SplitInput(Console.ReadLine()).Select(int.Parse);
    }

    private static IEnumerable<string> SplitInput(string input)
    {
        return Regex.Split(input, @"\s+")
                    .Where(x => !string.IsNullOrWhiteSpace(x));
    }
}
查看更多
We Are One
3楼-- · 2019-01-07 21:42
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SortInSubSet
{
    class Program
    {

        static int N, K;
        static Dictionary<int, int> dicElements = new Dictionary<int, int>();
        static void Main(string[] args)
        {

            while (!ReadNK())
            {
                Console.WriteLine("***************** PLEASE RETRY*********************");
            }

            var sortedDict = from entry in dicElements orderby entry.Key/3 , entry.Value ascending select entry.Value;

            foreach (int ele in sortedDict)
            {
                Console.Write(ele.ToString() + "  ");
            }

            Console.ReadKey();
        }

        static bool ReadNK()
        {
            dicElements = new Dictionary<int, int>();
            Console.WriteLine("Please entere the No. of element 'N' ( Between 2 and 9999) and Subset Size 'K' Separated by space.");

            string[] NK = Console.ReadLine().Split();

            if (NK.Length != 2)
            {
                Console.WriteLine("Please enter N and K values correctly.");
                return false;
            }

            if (int.TryParse(NK[0], out N))
            {
                if (N < 2 || N > 9999)
                {
                    Console.WriteLine("Value of 'N' Should be Between 2 and 9999.");
                    return false;
                }
            }
            else
            {
                Console.WriteLine("Invalid number: Value of 'N' Should be greater than 1 and lessthan 10000.");
                return false;
            }

            if (int.TryParse(NK[1], out K))
            {
                Console.WriteLine("Enter all elements Separated by space.");
                string[] kElements = Console.ReadLine().Split();

                for (int i = 0; i < kElements.Length; i++)
                {
                    int ele;

                    if (int.TryParse(kElements[i], out ele))
                    {
                        if (ele < -99999 || ele > 99999)
                        {
                            Console.WriteLine("Invalid Range( " + kElements[i] + "): Element value should be Between -99999 and 99999.");
                            return false;
                        }

                        dicElements.Add(i, ele);
                    }
                    else
                    {
                        Console.WriteLine("Invalid number( " + kElements[i] + "): Element value should be Between -99999 and 99999.");
                        return false;
                    }

                }

            }
            else
            {
                Console.WriteLine(" Invalid number ,Value of 'K'.");
                return false;
            }


            return true;
        }
    }
}
查看更多
倾城 Initia
4楼-- · 2019-01-07 21:46

You need something like (no error-checking code)

var ints = Console
            .ReadLine()
            .Split()
            .Select(int.Parse);

This reads a line, splits on whitespace and parses the split strings as integers. Of course in reality you would want to check if the entered strings are in fact valid integers (int.TryParse).

查看更多
冷血范
5楼-- · 2019-01-07 21:46
string x;
int m;
int n;

Console.WriteLine("Enter two no's seperated by space: ");

x = Console.ReadLine();
m = Convert.ToInt32(x.Split(' ')[0]);
n = Convert.ToInt32(x.Split(' ')[1]);

Console.WriteLine("" + m + " " + n);

This Should work as per your need!

查看更多
SAY GOODBYE
6楼-- · 2019-01-07 21:47
int a, b;
string line = Console.ReadLine();
string[] numbers= line.Split(' ');
a = int.Parse(numbers[0]);
b = int.Parse(numbers[1]);
查看更多
贪生不怕死
7楼-- · 2019-01-07 21:51

Then you should first store it in a string and then split it using the space as token.

查看更多
登录 后发表回答