我如何获得阅读线以返回课程?
我非常新/是编码的初学者。我正在Microsoft Visual Studio 2022中使用C#6.0。
我正在尝试创建某种搜索功能,但是也许有更好的方法可以做到这一点。
我创建了一个类(没有包含所有空间代码BC),并在这样的主程序/代码中给它
public void Attributes()
{
Console.WriteLine(affinity + ", " + aoe + ", " + effect + ", " + pointOfOrigin);
}
分配了变量
Spell FireBall = new Spell("Fire", "Sphere", "Explosion", "Thrown");
并 它返回:火,球体,爆炸,被抛弃。
我知道,如果您键入firball.attributes();它将返回属性,但是我不知道如何将用户输入调用属性。我的第一个猜测是,它与Console.Readline有关,并为其分配一个变量,但是我尝试过的所有内容都会转动错误。
正如我所说,我是初学者,任何帮助都非常感谢。如果我不够具体或使用错误的术语,我也很抱歉,因为我仍在学习所有其他内容的词汇。我的学习很有趣,所以请友善。
I am very new/ a beginner to coding. I'm using c# 6.0 in Microsoft Visual studio 2022.
I'm trying to create some sort of search function, but perhaps there is a better way to do it.
I created a class(didn't include all of the code bc of space) and gave it
public void Attributes()
{
Console.WriteLine(affinity + ", " + aoe + ", " + effect + ", " + pointOfOrigin);
}
and assigned variables in the main program/code like this
Spell FireBall = new Spell("Fire", "Sphere", "Explosion", "Thrown");
What I want to happen is to type the spell name(ex: Fireball) and then have it return the: Fire, Sphere, Explosion, Thrown.
I know that if you type FireBall.Attributes(); it will return the attributes, but I cannot figure out how to take user input to call the attributes. My first guess is that it has something to do with Console.ReadLine and assigning a variable to it but everything I've tried just turned errors.
As I said I'm a beginner and any help is much appreciated. I also apologize if I'm not being specific enough or used the wrong terms, as I'm still learning all of the vocabulary for the different stuff. I'm having a lot of fun learning, so please be nice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从我可以收集的内容来看,您要做的就是让用户输入关键字(在本例中的名称)并返回与该名称关联的对象,然后打印其属性(通过创建您已经完成的部分,
attrbiutes()
方法)。通过反射(硬,缓慢,荒谬)和词典(简单,快速,为此制作),有两种主要的方法。
我不会进入反射方法,因为它需要对我在stackoverflow答案中无法给您的语言有深刻的了解。
这是字典方法:
这是dotnet小提琴: https://dotnetfiddle.net/ib8w3a
底部,然后按Enter Input IT。


From what I can gather, what you're trying to do is have the user input a keyword (a name in this instance) and return an object associated with that name, then print its attributes (this part you've already done by creating the
Attrbiutes()
method).There are two main ways of doing this, by reflection (hard, slow, ridiculous), and by a dictionary (easy, FAST, made for this).
I will not get into the reflection method, as it requires a deep understanding of the language that I cannot give you in a Stackoverflow answer.
Here's the Dictionary method:
and here's the dotnet fiddle: https://dotnetfiddle.net/iB8w3a
Enter your input in the bottom, then press enter input it.


如果要打印出对象属性,则可以使用getProperties()方法。查看此示例是否对您有帮助。 PS:结果是您的咒语对象。
If you want to print out an object properties, You can use GetProperties() method. See if this example helps You. Ps: Result is the Spell object for You.