简体   繁体   English

如何在 C# 控制台中使用用户输入添加堆栈推送

[英]How to add a push in stack using a user input in C# console

Im stuck on adding a push when a user inputs a data.当用户输入数据时,我坚持添加推送。 The program functions this way like, when I enter a key like the letter A, it will add to the stack and show that the stack has 1 entity.该程序的功能是这样的,当我输入一个像字母 A 这样的键时,它会添加到堆栈中并显示堆栈有 1 个实体。 But im stuck, it should be when I enter another key it will add to the stack.但是我卡住了,应该是当我输入另一个键时它会添加到堆栈中。 Here is my code.这是我的代码。

while (true)
        {
            string letter = null;
            Console.WriteLine("Enter A = Apple, O = Orange, M = Mango, G = Guava");
            letter = Console.ReadLine();
            

            if (letter == "a")
            {
                Stack<string> myStack = new Stack<string>();
                myStack.Push(letter);
                Console.WriteLine("Fruits in the basket x " + myStack.Count);
                
               if(letter == "o") {
                    myStack.Push(letter);
                    Console.WriteLine("Fruits in the basket x " + myStack.Count);
                }

any suggestions and help will be greatly appreciated!任何建议和帮助将不胜感激! Thankyou in advance!先感谢您!

There are quite a few things wrong here.这里有很多问题。 Let's list them让我们列出它们

  1. Nested if s.嵌套if s。
    Your if(letter == "o") is nested inside the check if (letter = "a") which means it will always be false你的if(letter == "o")嵌套在 check if (letter = "a")这意味着它总是假的

  2. Console.ReadLine() returns after pressing Enter , not after pressing any key Console.ReadLine()在按下Enter后返回,而不是在按下任意键后返回
    Console.ReadLine() doesn't return each individual key press after they happened, it returns the entire sequence of characters (aka a string) pressed only after you pressed Enter Console.ReadLine()不会在发生后返回每个单独的按键,它仅在您按下Enter后返回按下的整个字符序列(又名字符串)

  3. Declaring your Stack inside the if statement for a声明你的筹码了,如果语句中a
    What if I want to add an orange as my first item?如果我想添加一个橙子作为我的第一个项目怎么办? This would throw an exception (if you fix your nested if issue) as it won't be defined when trying to access it.这将引发异常(如果您修复了嵌套的 if 问题),因为在尝试访问它时不会定义它。 It will also re-create the stack every time an apple is added, meaning, the stack will be cleared out and you'll never be able to have more than 1 Apple inside your stack.每次添加苹果时,它也会重新创建堆栈,这意味着堆栈将被清除,并且您的堆栈中永远不会有超过 1 个苹果。

  4. Repeated code重复代码
    Inside all your if statements is the exact same code, so there's no need for multiple statements, only one checking if the letter is any we accept在你所有的 if 语句中都是完全相同的代码,所以不需要多个语句,只需要一个检查字母是否是我们接受的

Here's how to fix them:以下是修复它们的方法:

// Declare myStack once, so it doesn't get reset every time we add an Apple
Stack<char> myStack = new Stack<char>();

while (true)
{
    Console.WriteLine("A = Apple, O = Orange, M = Mango, G = Guava");
    
    // Use Console.ReadKey not ReadLine to capture each key press separately
    // The 'true' parameter prevents the console from showing what character we pressed
    ConsoleKeyInfo keyPressed = Console.ReadKey(true);
    char characterPressed = keyPressed.KeyChar;
    
    if (characterPressed == 'a' || characterPressed == 'o' || characterPressed == 'm' || characterPressed == 'g')
    {
        myStack.Push(characterPressed);
    }
    else
    {
        Console.WriteLine("You pressed a non-valid character");
    }
    
    Console.WriteLine("Fruits in the basked x " + myStack.Count);
}

After you go into the if (letter == "a") statement you never reassign letter.在你进入if (letter == "a")语句之后,你永远不会重新分配字母。 So when it gets to if(letter == "o") letter still equals "a".所以当它到达if(letter == "o")字母仍然等于 "a"。

Above the second if statement you need to add letter = Console.ReadLine();在第二个 if 语句上方,您需要添加letter = Console.ReadLine(); as below:如下:

letter = Console.ReadLine();
if(letter == "o")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM