C#程序来检查有效值 [英] C# Program to check for valid values

查看:131
本文介绍了C#程序来检查有效值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,从下面的程序删除重复值。我不能使用数组以外的Hashset,列表或任何东西。我看过不同的解决方案,我可以使用和的Hashset名单,但我不允许使​​用的。有人可以帮助我。该方案是假设只需要5之间的值到j并删除任何重复值:

 类节目
{
静态无效的主要(字串[] args)
{
的char []数组=新的char [5];
Console.WriteLine(请输入5个字母/黑白只有通过记者:);
包含字母=ABCDEFGHIJ
的for(int i = 0; I< Array.Length)。
{
串低=到Console.ReadLine()ToLower将();
如果(letters.Contains(低级)及&放大器; lower.Length == 1)
{
数组[I] = Convert.ToChar(低级);
I ++;
}
,否则
{
Console.WriteLine(您输入了不正确的值);
继续;
}
}
Console.WriteLine(你输入下列输入:);
的for(int i = 0; I< Array.Length;我++)
{
Console.WriteLine(数组[I]);
}
}
}


解决方案

我认为这应该为你工作。

 静态无效的主要()
{
的char []数组=新的char [5];
Console.WriteLine(请输入5个字母b / W上至j只有:);
包含字母=ABCDEFGHIJ ;

INT计数器= 0;
而(计数器小于5)
{
字符串输入=到Console.ReadLine()ToLower将();
字符myChar =输入[0];!

如果(input.Length = 1 || array.Contains(myChar)|| !letters.Contains(myChar))
{
Console.WriteLine(你输入了不正确的值);
继续;
}

数组[计数器++] = myChar;
}

Console.WriteLine(你输入下列输入:);
Console.WriteLine(的string.join(,,阵列));
}


I need some help to remove duplicate values from the following program. I cannot use Hashset, list or anything except Arrays. I have looked at different solutions that i can use Hashset and list, but i am not allowed to use that. Can someone help me with this. The program is suppose to take only 5 values between a to j and remove any duplication values:

class Program
{
    static void main(string[] args)
    {
        char[] Array = new char[5];
        Console.WriteLine("Please Enter 5 Letters B/W a through j only: ");
        string letters = "abcdefghij";
        for (int i = 0; i < Array.Length;)
        {
                string lower = Console.ReadLine().ToLower();
                if (letters.Contains(lower) && lower.Length == 1)
                {
                    Array[i] = Convert.ToChar(lower);
                    i++;
                }
                else
                {
                    Console.WriteLine("You have entered an incorrect value);
                    continue;
                }
        }
        Console.WriteLine("You have Entered the following Inputs: ");
        for (int i = 0; i<Array.Length; i++)
        {
            Console.WriteLine(Array[i]);
        }
    }
}

解决方案

I think this should work for you.

static void Main()
    {
        char[] array = new char[5];
        Console.WriteLine("Please Enter 5 Letters B/W a through j only: ");
        string letters = "abcdefghij";

        int counter = 0;
        while (counter < 5)
        {
            string input = Console.ReadLine().ToLower();
            char myChar = input[0];

            if (input.Length != 1 || array.Contains(myChar) || !letters.Contains(myChar))
            {
                Console.WriteLine("You have entered an incorrect value");
                continue;
            }

            array[counter++] = myChar;
        }

        Console.WriteLine("You have Entered the following Inputs: ");
        Console.WriteLine(string.Join(", ", array));
    }

这篇关于C#程序来检查有效值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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