我如何获得阅读线以返回课程?

发布于 2025-02-01 14:21:15 字数 637 浏览 3 评论 0原文

我非常新/是编码的初学者。我正在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看春风乍起 2025-02-08 14:21:15

从我可以收集的内容来看,您要做的就是让用户输入关键字(在本例中的名称)并返回与该名称关联的对象,然后打印其属性(通过创建您已经完成的部分, attrbiutes()方法)。

通过反射(硬,缓慢,荒谬)和词典(简单,快速,为此制作),有两种主要的方法。

我不会进入反射方法,因为它需要对我在stackoverflow答案中无法给您的语言有深刻的了解。


这是字典方法:

using System;
using System.Collections.Generic;

//=== Spell class, this obviously isn't yours I'm just using this as an example
public class Spell {
    string affinity, aoe, effect, pointOfOrigin;
    
    public Spell(string affinity, string aoe, string effect, string pointOfOrigin) {
        this.affinity = affinity;
        this.aoe = aoe;
        this.effect = effect;
        this.pointOfOrigin = pointOfOrigin; 
    }
    
    public void Attributes()
    {
        Console.WriteLine(affinity + ", " + aoe + ", " + effect + ", " + pointOfOrigin);
    }
}

public class Program
{
    public static void Main()
    {
        //=== Create a dictionary of type Dictionary<string, Spell>. This means the dictionary has a string key and a Spell value for that key.
        Dictionary<string, Spell> Spells = new Dictionary<string, Spell>();
        
        //=== Add items to the dictionary, instead of setting a variable with its name, set the key to its name.
        // In this instance we're using names that are lowercase so we can do a case-insensitive search
        Spells["fireball"] = new Spell("Fire", "Sphere", "Explosion", "Thrown");
        
        Spells["freeze"] = new Spell("Ice", "Sphere", "Freeze", "Thrown");
        
        //=== Now here's where we ask the user what spell they'd like to print
        string input = Console.ReadLine().Trim().ToLower(); //=== Here we trim (remove whitespace from the start and end) and convert the input to lowercase.
        
        //=== Then we search for the spell in the dictionary
        if(Spells.ContainsKey(input)) {
            //=== If we get here, we found the spell
            Spell spell = Spells[input];
            
            //=== Print its attributes
            spell.Attributes();
        } else {
            //=== The spell was not found
            Console.WriteLine("Spell not found!");
        }
    }
}

这是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:

using System;
using System.Collections.Generic;

//=== Spell class, this obviously isn't yours I'm just using this as an example
public class Spell {
    string affinity, aoe, effect, pointOfOrigin;
    
    public Spell(string affinity, string aoe, string effect, string pointOfOrigin) {
        this.affinity = affinity;
        this.aoe = aoe;
        this.effect = effect;
        this.pointOfOrigin = pointOfOrigin; 
    }
    
    public void Attributes()
    {
        Console.WriteLine(affinity + ", " + aoe + ", " + effect + ", " + pointOfOrigin);
    }
}

public class Program
{
    public static void Main()
    {
        //=== Create a dictionary of type Dictionary<string, Spell>. This means the dictionary has a string key and a Spell value for that key.
        Dictionary<string, Spell> Spells = new Dictionary<string, Spell>();
        
        //=== Add items to the dictionary, instead of setting a variable with its name, set the key to its name.
        // In this instance we're using names that are lowercase so we can do a case-insensitive search
        Spells["fireball"] = new Spell("Fire", "Sphere", "Explosion", "Thrown");
        
        Spells["freeze"] = new Spell("Ice", "Sphere", "Freeze", "Thrown");
        
        //=== Now here's where we ask the user what spell they'd like to print
        string input = Console.ReadLine().Trim().ToLower(); //=== Here we trim (remove whitespace from the start and end) and convert the input to lowercase.
        
        //=== Then we search for the spell in the dictionary
        if(Spells.ContainsKey(input)) {
            //=== If we get here, we found the spell
            Spell spell = Spells[input];
            
            //=== Print its attributes
            spell.Attributes();
        } else {
            //=== The spell was not found
            Console.WriteLine("Spell not found!");
        }
    }
}

and here's the dotnet fiddle: https://dotnetfiddle.net/iB8w3a

Enter your input in the bottom, then press enter input it.
Searching for Fireball
Search found Fireball

影子的影子 2025-02-08 14:21:15

如果要打印出对象属性,则可以使用getProperties()方法。查看此示例是否对您有帮助。 PS:结果是您的咒语对象。

  var obj = new Result()
            {
                displayName = "Display Name",
                id = "1234",
                name = "Name",
                slug = "",
                 imageUrl = "url"
            };
            var props = obj.GetType().GetProperties();
            var sb = new StringBuilder();
            foreach (var p in props)
            {
                sb.AppendLine(p.Name + ", " + p.GetValue(obj, null));
            }
            Console.Write( sb.ToString());

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.

  var obj = new Result()
            {
                displayName = "Display Name",
                id = "1234",
                name = "Name",
                slug = "",
                 imageUrl = "url"
            };
            var props = obj.GetType().GetProperties();
            var sb = new StringBuilder();
            foreach (var p in props)
            {
                sb.AppendLine(p.Name + ", " + p.GetValue(obj, null));
            }
            Console.Write( sb.ToString());

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文