Home > Back-end >  I don't know what I'm doing wrong in my C# code
I don't know what I'm doing wrong in my C# code

Time:03-23

I am attempting to make a simple program that counts up one each time I press spacebar. I am very very new to writing software and most of this is completely foreign to me, so if you understand how I am thinking incorrectly I will welcome your insight!



using System;

namespace Counter
{
    class Program
    {
        static void Main(string[] args)
        {
            var tap = 0;

            while (Console.ReadKey(true).Key != ConsoleKey.Spacebar)
                tap  ;
            Console.WriteLine();
        }
    }
}

This is what I tried. The console shows (0) and then closes when I press spacebar. I don't know how to stop it from closing the console, it's as if all it has in the code body is a variable and "Console.WriteLine()".

CodePudding user response:

try this:

        var tap = 0;

        while (Console.ReadKey(true).Key != ConsoleKey.Spacebar)
        {
            tap  ;
            Console.WriteLine($"Key is not Spacebar, tap count = {tap}");
        }
        Console.WriteLine("You just press spacebar, now press enter to exit this console app");
        Console.ReadLine();//press enter to exit

CodePudding user response:

static void Main(string[] args)
{
    int tap = 0;

    while (Console.ReadKey(true).Key == ConsoleKey.Spacebar)
    {
        tap  ;
        Console.WriteLine("You pressed 'Spacebar' "   tap   "-time(s) so far.");
    }

    Console.WriteLine("You didn't press 'Spacebar' this time!");
    System.Threading.Thread.Sleep(3000); // wait 3 seconds so you actually have the chance to read the text in the console
}

Event though I would recommend something like this instead:

static void Main(string[] args)
{
    int tap = 0;

    while(true)
    {
        if(Console.ReadKey(true).Key == ConsoleKey.Spacebar)
        {
            tap  ;
            Console.WriteLine("You pressed 'Spacebar' "   tap   "-time(s) so far.");
        }
        else
        {
            Console.WriteLine("You didn't press 'Spacebar' this time!");
            System.Threading.Thread.Sleep(3000); // wait 3 seconds so you actually have the chance to read the text in the console
            break;
        }

    }
}

CodePudding user response:

I don't know if this is how to resolve a question in stackoverflow, but the answer was to make an if/elseif loop where Spacebar was an input that was counted and Enter was a Boolean value that closed the program when triggered to True.

{
        var tap = 0;
        bool quit = false;

        while (quit == false)
        {

            if (Console.ReadKey().Key == ConsoleKey.Spacebar)
            {
                tap  ;
                Console.WriteLine(tap);
            }
            else if (Console.ReadKey(true).Key == ConsoleKey.Enter)
            {
                quit = true;
            }
        }
    }

With the ElseIf I was able to count every trigger of Spacebar, whereas previously I had that statement within the If loop and it waited on the next keypress to see whether it was the Enter key or not.

Thanks for the help!

  •  Tags:  
  • c#
  • Related