新手问题(关于访问数组的)

Shryap 2009-09-26 02:24:53
本人想以写一个方法访问string[] str={"+","-","*","/"}这组数组,如果方法里所输入的字符串与数组里的某元素相同就返回true,否则返回一个false.请各路高手赐教。
...全文
75 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bingningwt 2009-09-26
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication4
{
class Program
{
public bool means(string a)
{
bool flag=true;
string[] str = new string[4] { "+", "-", "*", "/" };
int j;
for (int i = 0; i < a.Length; i++)
{
for (j = 0; j < 4; j++)
{
if ((a[i]) == (str[j]))
{
flag = true;
break;
}
else
flag = false;
}
}
Console.WriteLine("{0}", flag);
return flag;
}
static void Main(string[] args)
{
Program m=new Program();
string a = Console.ReadLine();
m.means(a);
Console.ReadLine();
}
}
}
SQL77 2009-09-26
  • 打赏
  • 举报
回复
INDEXOF()
CONTAINS()都行
lzsh0622 2009-09-26
  • 打赏
  • 举报
回复
public bool isExist(string s)
{
string[] str ={ "+", "-", "*", "/" };
return Array.IndexOf(str, s) >=0;
}
风之影子 2009-09-26
  • 打赏
  • 举报
回复
版主:真快,我刚才还看见了一个招聘贴,转眼就不在了.
风之影子 2009-09-26
  • 打赏
  • 举报
回复
1楼的方法小改一下:

        private bool Oper(string tempOper)
{
bool boolean = false;
string[] str ={ "+", "-", "*", "/" };
for (int count = 0; count < str.Length; count++)
{
if (str[count] == tempOper)
{
boolean = true;
break;
}
}
return boolean;
}
-过客- 2009-09-26
  • 打赏
  • 举报
回复
转为List,用List的Contains方法就可以了

List<string> list = new List<string>(new string[] { "+", "-", "*", "/" });
if (list.Contains(textBox1.Text))
richTextBox2.Text = "true";
else
richTextBox2.Text = "false";
风之影子 2009-09-26
  • 打赏
  • 举报
回复
        private bool Oper(string tempOper)
{
bool boolean = false;
string[] str ={ "+", "-", "*", "/" };
for (int count = 0; count < str.Length; count++)
{
if (str[count] == tempOper)
{
boolean = false;
break;
}
}
return boolean;
}

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧