简体   繁体   English

C# 控制台应用程序,用户输入资源并被告知需要什么垃圾

[英]C# Console Application where user enters resource and is told what Junk is required

Hoping someone can help.希望有人可以提供帮助。 I'm a very basic beginner in C#.我是 C# 的一个非常基础的初学者。 I set myself the task of creating a console application where I enter a resource I need ie Screw or screws into the console, hit enter and it retrieves all the junk from the list that contain that word, along with the amount of resources you would receive from the junk.我为自己设置了创建控制台应用程序的任务,在该应用程序中输入我需要的资源,即拧入或拧入控制台,按回车键,它会从包含该单词的列表中检索所有垃圾,以及您将收到的资源量从垃圾。 So Type Writer would be 2 screws.所以 Type Writer 是 2 个螺丝。 It's for Fallout 76 so I can see what junk contains what resource.它适用于 Fallout 76,所以我可以看到哪些垃圾包含哪些资源。

However I'm having a few issues:但是我有几个问题:

  1. The console only returns one result for screw, even though there are multiple results.控制台只返回一个螺丝结果,即使有多个结果。 (How do I resolve this and receive multiple results? ie Clip board and Toy Car) (我如何解决这个问题并收到多个结果?即剪贴板和玩具车)
  2. How do I search and get results for partial matches ie if I type scre or screws?我如何搜索并获得部分匹配的结果,即如果我输入了螺丝或螺丝?
  3. Is there a better way of creating a console application where you store values and a user searches for those values?有没有更好的方法来创建一个控制台应用程序,您可以在其中存储值并让用户搜索这些值? I cannot find anything close to what I need scouring the internet.我找不到任何接近我需要在互联网上搜索的东西。

Thank you for all the help.谢谢大家的帮助。

    using System;
using System.Collections.Generic;
class JunkList
{
    public string Resource { get; set; }
    public string Junk { get; set; }
    public int Amount { get; set; }

    public JunkList(string r, string j, int a)
    {
        this.Resource = r;
        this.Junk = j;
        this.Amount = a;
    }
}

class Program
{
    static void Main(string[] args)
    {
        string searchName;
        List<JunkList> infoList = new List<JunkList>();
        infoList.Add(new JunkList("Screw", "Type Writer", 2));
        infoList.Add(new JunkList("Screw", "Clip Board", 1));
        infoList.Add(new JunkList("Screw", "Toy Car", 3));
        Console.Write("Which resource do you want to search for?? \n");
        searchName = Console.ReadLine();
        for (int i = 0; i < infoList.Count; i++)
        {
            if (string.Compare(searchName, infoList[i].Resource, true) == 0)
            {
                Console.Write("Resource : " + infoList[i].Resource + "\n");
                Console.Write("Junk : " + infoList[i].Junk + "\n");
                Console.Write("Resource Amount : " + infoList[i].Amount + "\n");
                break;
            }
        }
        Console.ReadKey();
    }
}

The console only returns one result for screw, even though there are multiple results.控制台只返回一个螺丝结果,即使有多个结果。 (How do I resolve this and receive multiple results? ie Clip board and Toy Car) (我如何解决这个问题并收到多个结果?即剪贴板和玩具车)

Your issue is that you're breaking after finding the very first result:您的问题是您在找到第一个结果后就崩溃了:

if (string.Compare(searchName, infoList[i].Resource, true) == 0)
{
     Console.Write("Resource : " + infoList[i].Resource + "\n");
     Console.Write("Junk : " + infoList[i].Junk + "\n");
     Console.Write("Resource Amount : " + infoList[i].Amount + "\n");
     break; // REMOVE THIS LINE
}

break means that you're exiting from your for loop, so you're actually exiting as soon as you find a result. break意味着您正在退出for循环,因此您实际上是在找到结果后立即退出。

How do I search and get results for partial matches ie if I type scre or screws?我如何搜索并获得部分匹配的结果,即如果我输入了螺丝或螺丝?

You can change your if condition from您可以更改您的if条件

if (string.Compare(searchName, infoList[i].Resource, true) == 0)

to something like类似于

if (infoList[i].Resource.ToLowerInvariant().Contains(searchName.ToLowerInvariant()))

This checks if the inserted string is contained in one of the strings that you have in your list.这将检查插入的字符串是否包含在列表中的字符串之一中。 The ToLowerInvariant is used to make the search case-insensitive. ToLowerInvariant用于使搜索不区分大小写。

Of course, this is not a fuzzy-search, so searching for screws won't lead to any result.当然,这不是模糊搜索,所以搜索screws不会导致任何结果。 Doing fuzzy searches is a little bit more complex and it's probably outside the scope of this question.进行模糊搜索有点复杂,它可能不在这个问题的 scope 范围内。

Is there a better way of creating a console application where you store values and a user searches for those values?有没有更好的方法来创建一个控制台应用程序,您可以在其中存储值并让用户搜索这些值? I cannot find anything close to what I need scouring the internet.我找不到任何接近我需要在互联网上搜索的东西。

Opinion-based questions are not meant for StackOverflow, and asking a better way to do something is one of those.基于意见的问题不适用于 StackOverflow,而要求更好的方法来做某事就是其中之一。 I don't think you'll get an answer on this.我不认为你会得到这个答案。

To make it a less subjective, I'm going to interpret your last question as "Is there a common software pattern or approach that is used for storing data objects that can then be easily searched?"为了减少主观性,我将您的最后一个问题解释为“是否有一种通用的软件模式或方法用于存储可以轻松搜索的数据对象?”

In that regard, you're describing features and capabilities that a database is really good at, specifically storing some table of data and providing a fast way to search (query) it using criteria that you define.在这方面,您正在描述数据库真正擅长的特性和功能,特别是存储一些数据表并提供使用您定义的标准搜索(查询)它的快速方法。

C# and the .NET frameworks offer some pretty in-depth and robust tools for leveraging databases and managing mappings between data classes like JunkList and database tables; C# 和 .NET 框架提供了一些非常深入和强大的工具,用于利用数据库和管理数据类(如JunkList和数据库表)之间的映射; these types of tools are called ORMs, or Object Relational Mappers, and you'll see reference to them scattered throughout Microsoft's documentation, especially Entity Framework.这些类型的工具称为 ORM 或 Object 关系映射器,您会在 Microsoft 的文档中看到对它们的引用,尤其是实体框架。

When getting started, more complicated ORMs can get a bit overwhelming, so it's not a bad idea to start small.开始时,更复杂的 ORM 可能会有点不知所措,所以从小处着手并不是一个坏主意。 Check out some very simple ORMs that use something like SQLite, which just live in a file and require minimal dependencies (the act of calling the database actually can be enough to create it.).查看一些非常简单的 ORM,它们使用 SQLite 之类的东西,它们只存在于一个文件中并且需要最少的依赖项(调用数据库的行为实际上足以创建它。)。

You might take a look at https://github.com/praeclarum/sqlite-net , which is very easy to set up in a matter of minutes and start using with just the basics provided in its documentation.您可以查看https://github.com/praeclarum/sqlite-net ,它很容易在几分钟内完成设置,并且只需使用其文档中提供的基础知识即可开始使用。

  1. The reason you only get one result back is because of the break statement.你只得到一个结果的原因是因为break语句。 It cancels the for loop.它取消了for循环。 Try removing it.尝试删除它。

  2. Try looking at the String.Contains() method.尝试查看String.Contains()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在计算机上运行ac#控制台应用程序需要什么 - What is required to run a c# console application on a computer 试图在 c# 中制作它,用户输入他们的年龄并告诉他们他们出生的年份 - Trying to make it in c# where the user enters their age and it tells them what year they were born in PackageReference 在控制台应用程序 C# 中的位置 - Where is PackageReference located in console application C# C#控制台应用程序:在用户输入有效数据之前,如何继续返回错误消息? - C# console app: How can I continue returning an error message until user enters valid data? 简单的切换用户c#控制台应用程序 - simple switch user c# console application 在 C# 中退出控制台应用程序的命令是什么? - What is the command to exit a Console application in C#? C#应用程序中资源和嵌入式资源之间的区别是什么? - What's the difference between a Resource and an Embedded Resource in a C# application? 在 c# 控制台应用程序和控制台输出中记录用户输入 - Log user input in a c# console application as well as console out C#控制台应用程序 - C# console application 客户端C#应用程序需要哪些软件先决条件? - What software prerequisite are required for a C# application at client side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM