using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Game { class Class2 { public void Say() { string[] item = new string[3] { "石头", "剪刀", "布" }; Random r = new Random(); string fi1 = item[r.Next(item.Length)]; Console.WriteLine(fi1); } } }
随机产生的一个数,怎样在主程序中调用 这个类产生的值?
你不想返回string也可以用out参数,
string fi1=string.Empty;
public void Say(out fi1)
{
string[] item = new string[3] { "石头", "剪刀", "布" };
Random r = new Random();
fi1 = item[r.Next(item.Length)];
Console.WriteLine(fi1);
}
public stringSay()
{
string[] item = new string[3] { "石头", "剪刀", "布" };
Random r = new Random();
string fi1 = item[r.Next(item.Length)];
Console.WriteLine(fi1);
return fi1;
}
string value=new Class2().Say();
value就是值
void了怎么取,又不是全局的 按楼上的
把你Say()改成
public static string Say()
{
string[] item = new string[3] { "石头", "剪刀", "布" };
Random r = new Random();
string fi1 = item[r.Next(item.Length)];
return fi1
}
在主程序中写:
string item=new Class2.Say();
console.WriteLine(item);