如何在c#中将用户输入转换为int数组

时间:2015-11-22 11:18:14

标签: c# arrays

我发现回答这个问题的最接近的事情是使用for循环将几个字符串输入转换为数组。

我只想取1个字符串并将前7个数字转换为int数组。

此代码采用字符的整数值,然后根据Unicode值对它们进行测试,如果有效则返回true,否则重新生成while循环并再次请求输入。当我使用Console.Read();方法,并首先输入一个无效值,它会说我的代码在7次迭代中无效。这意味着即使字符串具有有效输入,console.Read()也必须再运行7次。

public static void GetDigits(ref int[] callNumberArray, ref bool valid)
        {
        Console.WriteLine("Please enter the code you wish to dial.");
        while ( valid == false)
        {//This loop will reiterate the read() function if the code is not valid.
            valid = true;
            for (int i = 0; i < 7; i++ )
            {
                if (i != 6 && i!= 5 && i != 5 && i != 4 && i != 3 && i != 2 && i != 1 && i != 0)
                {
                    i = 0;
                }
                callNumberArray[i] = Console.Read();// I want to change this

            }
            for (int i = 0; i < 7; i++)
            {
                if (i != 6 && i != 5 && i != 5 && i != 4 && i != 3 && i != 2 && i != 1 && i != 0)
                {
                    i = 0;
                }
                if (callNumberArray[0] == 53)
                {
                    valid = false;
                }
                if (callNumberArray[i] < 49)
                {
                    valid = false;
                }
                if (callNumberArray[i] > 57 && callNumberArray[i] < 65)
                {
                    valid = false;
                }
                if (callNumberArray[i] > 90 && callNumberArray[i] < 97)
                {
                    valid = false;
                }
                if (callNumberArray[i] > 122)
                {
                    valid = false;
                }
            }
            if (valid == false)
            {
                Console.WriteLine("You entered an invalid code. Please re-enter your code.");
            }

        }

5 个答案:

答案 0 :(得分:3)

我认为你应该使用正则表达式,例如:

MatchCollection matchList = Regex.Matches(Content, Pattern);
var list = matchList.Cast<Match>().Select(match => match.Value).ToList();

答案 1 :(得分:2)

  

我只想取1个字符串并将前7个数字转换为int数组。

     for(int i = 0; i < n; i++){
            obj = objectsArray[i];
            objBox = obj.BoundingBox;
            if ((B.Max.Z > objBox.Max.Z || B.Max.Z > objBox.Min.Z) && (B.Min.Z < objBox.Max.Z || B.Min.Z < objBox.Min.Z) && (B.Max.X > objBox.Max.X || B.Max.X > objBox.Min.X) && (B.Min.X < objBox.Max.X || B.Min.X < objBox.Min.X) && (B.Max.Y > objBox.Max.Y || B.Max.Y > objBox.Min.Y) && (B.Min.Y < objBox.Max.Y || B.Min.Y < objBox.Min.Y)) {
                // obj is  inside or overlapping box B
                ObjectsInsideB.add(obj) 
             } else{
                // obj out of box B
             }
      }
// objBox.Max bounding box vertex at maximum end
// objBo.min bounding box vertex at minimum end

基本上,如果你想逐字逐句地做到这一点,那么使用{{1来检查一个数字要容易得多或者正则表达式string subString = Console.ReadLine().Substring(0,7); //Check if the whole string is a parsable number if(int.TryParse(subString) == false) { Console.WriteLine("Not a valid number..."); return; } //convert it an int[] int[] values = subString.ToCharArray().Select( value => int.Parse(value.ToString())).ToArray(); (每个字符),如果你对此很有帮助。

答案 2 :(得分:1)

不确定&#34;算法&#34;你写了,我同意谢尔盖别列佐夫斯基的看法,看起来很奇怪,无论如何这应该回答你的具体问题:

public static void GetDigits(ref int[] callNumberArray, ref bool valid)
    {
    Console.WriteLine("Please enter the code you wish to dial.");
    while ( valid == false)
    {//This loop will reiterate the read() function if the code is not valid.
        valid = true;
        for (int i = 0; i < 7; i++ )
        {
            if (i != 6 && i!= 5 && i != 5 && i != 4 && i != 3 && i != 2 && i != 1 && i != 0)
            {
                i = 0;
            }
            callNumberArray[i] = Console.Read();// I want to change this

        }
        for (int i = 0; i < 7; i++)
        {
            if(!valid) break;
            if (i != 6 && i != 5 && i != 5 && i != 4 && i != 3 && i != 2 && i != 1 && i != 0)
            {
                i = 0;
            }
            if (callNumberArray[0] == 53)
            {
                valid = false;
            }
            if (callNumberArray[i] < 49)
            {
                valid = false;
            }
            if (callNumberArray[i] > 57 && callNumberArray[i] < 65)
            {
                valid = false;
            }
            if (callNumberArray[i] > 90 && callNumberArray[i] < 97)
            {
                valid = false;
            }
            if (callNumberArray[i] > 122)
            {
                valid = false;
            }
        }
        if (valid == false)
        {
            Console.WriteLine("You entered an invalid code. Please re-enter your code.");
        }

    }

答案 3 :(得分:1)

这是一种方法,提示用户输入电话号码,拒绝无效字符,并显示当前电话号码和占位符,以便输入数字:

var nice = $("html").niceScroll({
    scrollspeed: 60,
    zindex: "999999",
    background: "#fff",
    mousescrollstep: 40,
    cursoropacitymin: 0,
    cursoropacitymax: .5,
    cursorwidth: 15,
    cursorborder: 0,
    cursorcolor: '#000',
    cursorborderradius: 6,
    autohidemode: false,
    horizrailenabled: false

});

提示输入:

private static int[] GetPhoneNumber(int phoneLength = 7)
{
    List<int> phoneNumbers = new List<int>();

    while (true)
    {
        EditorFor("Phone", String.Concat(phoneNumbers), phoneLength);

        var key = Console.ReadKey(intercept: true);
        if (key.Key == ConsoleKey.Escape) 
            return new int[0]; // return empty array if user cancelled input

        var c = key.KeyChar;
        if (!Char.IsDigit(c))
            continue;

        phoneNumbers.Add(Int32.Parse(c.ToString()));
        if (phoneNumbers.Count == phoneLength)
        {
            EditorFor("Phone", String.Concat(phoneNumbers), phoneLength);
            return phoneNumbers.ToArray();
        }
    }
}

用法:

private static void EditorFor(string label, string value, int length)
{
    Console.SetCursorPosition(0, Console.CursorTop);
    Console.Write(new String(' ', Console.WindowWidth));
    Console.SetCursorPosition(0, Console.CursorTop);

    int charactersLeftToInput = length - value.Length;
    string placeholder = new String('*', charactersLeftToInput);

    Console.Write("{0}: {1}{2}", label, value, placeholder);
    Console.CursorLeft -= charactersLeftToInput;
}

控制台:

enter image description here

答案 4 :(得分:0)

好的,经过深思熟虑,我决定为数组中的每个元素使用一个单独的while ( valid == false) { valid = true; callNumberArray[0] = Console.Read(); callNumberArray[1] = Console.Read(); callNumberArray[2] = Console.Read(); callNumberArray[3] = Console.Read(); callNumberArray[4] = Console.Read(); callNumberArray[5] = Console.Read(); callNumberArray[6] = Console.Read(); //etc etc... ,现在它正在做我想做的事。

<div class="container">
  <div class="left-sidebar">
    I am menu
  </div>
  <div class="right-sidebar">
    I am additional content
  </div>
  <div class="content">
    I am content
  </div>
</div>