C# 从另一个文件C调用方法#
我无法调用我的方法。我有两个单独的文件。当用户键入S时,将调用我的其他文件中的shake方法。因此,当用户得到答案时,它将是随机的 我对如何在另一个文件中引入该方法感到困惑。下面是两个文件 Program.cs:C# 从另一个文件C调用方法#,c#,methods,C#,Methods,我无法调用我的方法。我有两个单独的文件。当用户键入S时,将调用我的其他文件中的shake方法。因此,当用户得到答案时,它将是随机的 我对如何在另一个文件中引入该方法感到困惑。下面是两个文件 Program.cs: static void Main(string[] args) { Console.WriteLine("Main program!"); Console.WriteLine("Welcome to the Magic 8 Ball"); Console.
static void Main(string[] args)
{
Console.WriteLine("Main program!");
Console.WriteLine("Welcome to the Magic 8 Ball");
Console.WriteLine("What would you like to do?");
Console.WriteLine("(S)hake the Ball");
Console.WriteLine("(A)sk a Question");
Console.WriteLine("(G)et the Answer");
Console.WriteLine("(E)xit the Game");
Magic8Ball_Logic.Magic8Ball ball = new Magic8Ball_Logic.Magic8Ball();
string input = Console.ReadLine().ToUpper();
public static string userAnswer = "";
do
{
if (input == "S")
{
if (userAnswer != null)
{
Console.WriteLine("Searching the Mystic Realms(RAM) for the answer");
}
else
{
//Call Method Shake()
}
}
else if (input == "A") {
userAnswer = Console.ReadLine();
}
else if (input == "G") {
//Call Method GetAnswer()
}
} while (input != "E");
}
Magic8Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Magic8Ball_Logic
{
public class Magic8Ball
{
private List<string> _answers;
private int _currentIndex;
private string randomString;
public Magic8Ball()
{
_answers = new List<string>();
_answers.Add("It is certain.");
_answers.Add("It is decidedly so.");
_answers.Add("Without a doubt.");
}
public Magic8Ball(List<string> answers)
{
//I won't use the 20 default. use the ones passed in .
_answers = answers;
}
public void Shake()
{
//picking the index of the answer to show the user
Random r = new Random();
int index = r.Next(_answers.Count);
randomString = _answers[index];
}
public string GetAnswer()
{
//using the index picked by shake to return the answer
//return "";
return randomString;
}
public int AnswerCount
{
get { return _answers.Count; }
}
/* public override string ToString()
{
foreach (var el in _answers)
{
return el;
}
}*/
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间Magic8Ball_逻辑
{
公共类Magic8Ball
{
私人名单(答案),;
私有int_currentIndex;
私有字符串随机字符串;
公共Magic8Ball()
{
_答案=新列表();
_答案。加上(“这是肯定的。”);
_答案。加上(“确实如此。”);
_答案。加上(“毫无疑问”);
}
公共Magic8Ball(列出答案)
{
//我不会使用默认值20。请使用传入的值。
_答案=答案;
}
公共图书馆
{
//选择答案的索引以显示给用户
随机r=新随机();
int index=r.Next(_answers.Count);
随机字符串=_答案[索引];
}
公共字符串GetAnswer()
{
//使用shake拾取的索引返回答案
//返回“”;
返回随机字符串;
}
公共内部应答计数
{
获取{return\u answers.Count;}
}
/*公共重写字符串ToString()
{
foreach(答案中的变量)
{
返回el;
}
}*/
}
}
首先必须创建此类的对象,然后调用该方法
**Edit**
ball.Shake();
正如这里所写的:
您的Program.cs应更新如下:
- 替换此评论
ball.Shake()代码>
- 替换此评论
//使用调用方法GetAnswer()
ball.GetAnswer()代码>你能提供Magic8Ball.cs的完整代码吗?Magic8Ball类中的shake方法是吗?@AndrewPatynko刚刚补充说,不要把它们当作简单的文件,并从类的角度考虑它是在这里创建的。WriteLine(“E)退出游戏”);Magic8Ball_Logic.Magic8Ball ball=新的Magic8Ball_Logic.Magic8Ball();字符串输入=Console.ReadLine().ToUpper();