方法';没有过载;存在';采取';1';论点

本文关键字:论点 采取 没有过 方法 存在 | 更新日期: 2023-09-27 18:27:38

我正在制作一个程序,该程序获取用户的用户名、年龄和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();
        }
    }
}

感谢您的帮助。

方法';没有过载;存在';采取';1';论点

替换函数的这一部分

        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;
        }

欢呼!

也许可以试试这个代码:

        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;
            }
        }

因为你把字符串和字符混合在一起,你需要把你的数组改成字符数组,然后你可以检查它是否包含禁止的符号

你也可以修改你的代码一点,以删除无用的循环:

        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;

我不确定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"));

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

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