我正在创建一个程序,它接受用户的用户名,年龄和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();
}
}
}
感谢任何帮助。
答案 0 :(得分: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;
}
喝彩!
答案 1 :(得分: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;
}
}
因为你正在混合字符串和字符,你需要将数组更改为字符数组,然后你可以检查它是否包含禁止的符号
你也可以稍微修改你的代码以删除无用的循环:
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;
答案 2 :(得分: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"));
答案 3 :(得分:1)
另一种选择是使用String.IndexOfAny()方法,它将char数组作为参数,如:
public static bool check_for_symbols(string s)
{
return ("!@#$%^&*() -+=~`\"'{}[]\\:;<>?/,".IndexOfAny(s.ToCharArray()) > -1);
}