0

我正在制作一个程序,它获取用户的用户名、年龄和 ID,然后将它们打印到屏幕上。用户名不能包含任何符号或空格(_ 除外)。所以,我创建了一个函数,true如果名称中有符号,则返回,false如果没有。但是我在编译过程中遇到错误:No overload for method 'Exists' takes '1' arguments. 完整的错误:

challenge_2.cs(23,37): error CS1501: No overload for method `Exists' takes `1' arguments
/usr/lib/mono/2.0/mscorlib.dll (Location of the symbol related to previous error)
Compilation failed: 1 error(s), 0 warnings

这是代码:

using System;
using System.Collections.Generic;

public class Challenge_2
{
    static string myName;
    static string myAge;
    static string myUserID;
    public static char[] break_sentence(string str)
    {
        char[] characters = str.ToCharArray();
        return characters;
    }
    public static bool check_for_symbols(string s)
    {
        string[] _symbols_ = {"!","@","#","$","%","^","&","*","(",")"," ","-","+","=","~","`","\"","'","{","}","[","]","\\",":",";","<",">","?","/",","};
        List<string> symbols = new List<string>(_symbols_);
        char[] broken_s = break_sentence(s);
        int _bool_ = 0;
        for(int i = 0; i < symbols.Count; i++)
        {
            string current_symbol = symbols[i];
            if(broken_s.Exists(current_symbol))
            {
                _bool_ = 1;
                break;
            }
        }
        if(_bool_ == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    public static void Main()
    {
        Console.WriteLine("Please answer all questions wisely.");
        Console.WriteLine(" ");
        name();
        Console.WriteLine(" ");
        age();
        Console.WriteLine(" ");
        userID();
        Console.WriteLine(" ");
        string nextAge = Convert.ToString(Convert.ToInt32(myAge)+1);
        string nextID = Convert.ToString(Convert.ToInt32(myUserID)+1);
        Console.WriteLine("You are {0}, aged {1} next year you will be {2}, with user id {3}, the next user is {4}.", myName, myAge, nextAge, myUserID, nextID);
    }
    public static void name()
    {
        Console.WriteLine("What is your forum name?");
        Console.Write(">> ");
        myName = Console.ReadLine();
        while(check_for_symbols(myName) == true)
        {
            Console.WriteLine("Name can't contain symbols/spaces.");
            Console.Write("Please enter a valid forum name: ");
            myName = Console.ReadLine();
        }
    }
    public static void age()
    {
        Console.WriteLine("What is your age?");
        Console.Write(">> ");
        myAge = Console.ReadLine();
        while(Convert.ToInt32(myAge) <= 0 || Convert.ToInt32(myAge) > 120)
        {
            Console.WriteLine("That isn't a valid age.");
            Console.Write("Please enter a valid age: ");
            myAge = Console.ReadLine();
        }
    }
    public static void userID()
    {
        Console.WriteLine("What is your User ID?");
        Console.Write(">> ");
        myUserID = Console.ReadLine();
        while(Convert.ToInt32(myUserID) <= 0 || Convert.ToInt32(myUserID) > 999999)
        {
            Console.WriteLine("UserID must be in the range: 0 < x < 1000000.");
            Console.Write("Please enter a valid user ID: ");
            myUserID = Console.ReadLine();
        }
    }
}

任何帮助表示赞赏。

4

4 回答 4

5

替换这部分功能

        string current_symbol = symbols[i];
        if(broken_s.Exists(current_symbol))
        {
            _bool_ = 1;
            break;
        }

进入

        string current_symbol = symbols[i];
        if(broken_s.Contains(current_symbol))
        {
            _bool_ = 1;
            break;
        }

干杯!

于 2013-08-13T10:50:30.437 回答
2

也许试试这个代码:

        char[] _symbols_ = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', ' ', '-', '+', '=', '~', '`', '\'', '\'', '{', '}', '[', ']', '\\', ':', ';', '<', '>', '?', '/', ',' };
        List<char> symbols = new List<char>(_symbols_);
        char[] broken_s = break_sentence(s);
        int _bool_ = 0;
        for (int i = 0; i < symbols.Count; i++)
        {
            char current_symbol = symbols[i];
            if (broken_s.Any(x=>x==current_symbol))
            {
                _bool_ = 1;
                break;
            }
        }

因为您正在混合字符串和字符,所以您需要将数组更改为 char 数组,然后您可以检查它是否包含禁止符号

您还可以稍微修改代码以删除无用的循环:

        List<char> symbols = new List<char>(_symbols_);
        char[] broken_s = break_sentence(s);
        int _bool_ = 0;
        if(broken_s.Any(x=>symbols.Contains(x)) _bool=1;
于 2013-08-13T10:49:59.117 回答
1

另一种选择是使用String.IndexOfAny () 方法,该方法将 char 数组作为参数,例如:

    public static bool check_for_symbols(string s)
    {

        return ("!@#$%^&*() -+=~`\"'{}[]\\:;<>?/,".IndexOfAny(s.ToCharArray()) > -1);

    }
于 2013-08-13T11:23:25.353 回答
1

我不确定 Mono 但在 Microsoft.NET 中 Exists 的签名是:

T[] array, Predicate<T>

这意味着您可以这样使用它:

    var testCharArray = new[] {'a','b'};
    var condition = Array.Exists(testCharArray, c => c.Equals('b'));

这也适用于字符串:

    var testStringArray = new[] { "anders", "calle" };
    var condition2 = Array.Exists(testStringArray, c => c.Equals("calle"));
于 2013-08-13T10:50:52.087 回答